Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
compare content of two objects using Object.entries VS for...in
(version: 1)
Comparing performance of:
Object.entries vs Object for..in vs Object for..in #2 vs Object for..in #3
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function makeid() { var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (var i = 0; i < 5; i++) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; } window.parentObj = {}; const samples = 1000; const a = {}; for (let i = 0; i < samples; i++) { a[makeid()] = makeid(); } const b = JSON.parse(JSON.stringify(a)); const c = {}; for (let i = 0; i < samples; i++) { c[makeid()] = makeid(); } window.parentObj = {a: a, b: b, c: c};
Tests:
Object.entries
const a = window.parentObj.a; const b = window.parentObj.b; const entries = Object.entries(a); const isNotEqual = entries.some(([k, v], i) => b[k] != v); if (isNotEqual) return false; return entries.length === Object.keys(b).length;
Object for..in
const a = window.parentObj.a; const b = window.parentObj.b; for(var key in a) { if (a[key] != b[key]) return false; }; return Object.keys(a).length === Object.keys(b).length;
Object for..in #2
const a = window.parentObj.a; const b = window.parentObj.b; let lengthA = 0; for(var key in a) { lengthA = lengthA + 1; if (a[key] != b[key]) return false; }; return lengthA === Object.keys(b).length;
Object for..in #3
const a = window.parentObj.a; const b = window.parentObj.b; let lengthA = 0; for(var key in a) { lengthA = lengthA + 1; if (a[key] != b[key]) return false; }; let lengthB = 0; for(var key in b) { lengthB = lengthB + 1; }; return lengthA === lengthB;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Object.entries
Object for..in
Object for..in #2
Object for..in #3
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):
**Benchmark Overview** The provided benchmark compares the performance of three approaches to compare the content of two objects: `Object.entries`, `for...in`, and their variations (`#2` and `#3`). The test creates two large objects, `a` and `b`, with randomly generated keys and values. It then checks if the two objects have equal keys and values. **Test Options** 1. **Object.entries**: This method returns an array of a given object's own enumerable string-keyed property [key, value] pairs. 2. **for...in**: This loop iterates over the properties (keys) of an object. 3. **Object for...in #2**: Similar to `#1`, but returns the length of the `a` object after iteration. 4. **Object for...in #3**: Compares the lengths of the two objects' keys before and after iteration. **Pros and Cons** * **Object.entries**: Fast, concise, and modern way to compare objects. However, it may not be supported in older browsers or Node.js versions. * **for...in**: Traditional method for iterating over object properties. It is widely supported but can be slower than `Object.entries` due to the overhead of checking property existence. * **Object for...in #2** and **#3**: These variations are similar to `for...in`, but add an extra step to compare lengths, which can improve performance by reducing the number of iterations. **Library Used** None. The benchmark does not use any external libraries, relying on native JavaScript features. **Special JS Features or Syntax** * None mentioned in the provided code snippets. **Benchmark Result Interpretation** The latest benchmark result shows that: * `Object.entries` is the fastest approach, with an execution rate of approximately 2186 executions per second. * The variations of `for...in` (`#2` and `#3`) are slower than `Object.entries`, but closer in performance to each other. **Alternative Approaches** Other methods to compare object contents could include: * Using `JSON.stringify()` and comparing the resulting strings ( slower due to stringification overhead). * Implementing a custom loop using `Array.prototype.forEach()` or `Array.prototype.reduce()`. * Utilizing libraries like Lodash or Moment.js for more complex comparisons. Keep in mind that the choice of approach depends on the specific use case, performance requirements, and browser/Node.js support.
Related benchmarks:
Object values: Object.entries VS Object.keys VS Object.keys with extra array VS Object.entries without array VS Object values: Object.entries loop for
Object values: Object.entries VS Object.keys VS Object.keys with extra array VS Object.entries without array VS for .. of
Object.entries VS Object.keys VS Object.keys with extra array VS Object.entries without array VS Array
Object.entries VS Object.keys VS Object.keys with extra array VS Object.entries without array VS Object.keys as separate array with for loop
Object.entries VS Object.keys VS Object.values length check
Comments
Confirm delete:
Do you really want to delete benchmark?