Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
schema vs manual
(version: 0)
Comparing performance of:
hp vs hps vs ref
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function hasProp(obj, prop, type) { return (Object.prototype.hasOwnProperty.call(obj, prop) &&typeof obj[prop] === type); } function hasProps(obj, schema) { for (const [prop, type] of Object.entries(schema)) { if (!(prop in obj && typeof obj[prop] === type)) { return false; } } return true; } function hasProps2(obj, schema) { const entries = Object.entries(schema); for (let i = 0, length = entries.length; i < length; ++i) { if (!(entries[i][0] in obj && (entries[i][1] === 'array' ? Array.isArray(obj[entries[i][0]]) : typeof obj[entries[i][0]] === entries[i][1]))) { return false; } } return true; } const testTypes = ['undefined', 'object', 'boolean', 'number', 'bigint', 'string', 'symbol', 'function', 'array']; const o = { a: undefined, b: {}, c: true, c2: false, d: -34, e: BigInt(56), f: 'yass', f2: '', g: Symbol('queen'), h: (a, b) => a + b.length, i: [1, 32], }; function cartesianProduct(...allEntries) { return allEntries.reduce((results, entries) => results .map((result) => entries.map((entry) => [...result, entry])) .reduce((subResults, result) => [...subResults, ...result], []), [[]]); } const testParams = cartesianProduct(Object.keys(o), testTypes); const schema = { a: 'undefined', f: 'string', g: 'symbol', i: 'array' } function hp() { if (Math.round(Math.random())) { return hasProp(o, "a", 'undefined') && hasProp(o, "f", 'string') && hasProp(o, "g", 'symbol') && hasProp(o, "i", 'object'); } else { return true } } function hps() { if (Math.round(Math.random())) { return hasProps(o, schema); } else { return true } } function ref() { if (Math.round(Math.random())) { return "a" in o && typeof o.a === 'undefined' && "f" in o && typeof o.f === 'string' && "g" in o && typeof o.g === 'symbol' && "i" in o && typeof o.i === 'object'; } else { return true } }
Tests:
hp
let r1 = false; r1 = hp(); const r = `${r1} + 4`;
hps
let r2 = false; r2 = hps(); const r = `${r2} + 4`;
ref
let r3 = false; r3 = ref(); const r = `${r3} + 4`;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
hp
hps
ref
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of JavaScript code is crucial for optimizing and improving application performance. **Benchmark Overview** The provided benchmark, `schema vs manual`, compares three approaches to validate object properties: 1. **Manual Approach (hp)**: This function checks each property individually using `hasProp`. 2. **Schema-Based Approach (hps)**: This function uses a schema to validate all properties at once. 3. **Reference Approach (ref)**: This function uses the reference implementation, which is not explicitly defined in the benchmark, but seems to be related to checking if certain properties exist and have specific types. **Options Compared** The three approaches are compared in terms of performance: * Execution frequency per second (`ExecutionsPerSecond`) * Browser, device platform, operating system, and raw UA string (indicative of the browser's version and architecture) **Pros and Cons of Each Approach** 1. **Manual Approach (hp)**: * Pros: Simple to implement and understand. * Cons: Can be slow for large objects due to individual property checks. 2. **Schema-Based Approach (hps)**: * Pros: Efficient for large objects, as it only needs to iterate over the schema once. * Cons: Requires a predefined schema, which can be difficult to maintain and update. 3. **Reference Approach (ref)**: * Pros: Optimized for performance, likely using internal implementation details not exposed in the benchmark. * Cons: Difficult to understand and implement without knowledge of Chrome's internals. **Library Usage** None of the provided functions use external libraries. **Special JS Features or Syntax** The benchmark uses `Math.round(Math.random())` to introduce randomness in the execution order, making it a microbenchmark. This allows measuring performance with a high degree of accuracy. **Alternatives** Other approaches for validating object properties could be: * Using `Object.keys()` and `every()` * Implementing a custom validation function using `Object.prototype.hasOwnProperty.call()` * Utilizing modern JavaScript features like `for...of` loops or `Array.prototype.every()`
Related benchmarks:
undefined vs. typeof vs. in vs. hasOwnProperty
undefined vs. typeof vs. in vs. hasOwnProperty 2
undefined vs. typeof vs. in vs. hasOwnProperty for non-existing property
undefined vs. typeof vs. in vs. hasOwnProperty 222
in vs Object.hasOwn vs Object.prototype.hasOwnProperty
Comments
Confirm delete:
Do you really want to delete benchmark?