Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
object.entries vs object.keys vs for in
(version: 0)
Comparing performance of:
Object.entries vs for in vs Object.keys
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { a: 1, b: 2, c: 3, d: 4, 0: 'a', 1: 'b', str: 'long string', nested: { nested: 0 }, hello: 55.5 };
Tests:
Object.entries
for (let i = 0; i < 10000; i++) { for (let entries = Object.entries(obj), i = entries.length - 1; i >= 0; i--) { console.log(entries[i]) } }
for in
for (let i = 0; i < 10000; i++) { for (let key in obj) { if (obj.hasOwnProperty(key)) { console.log([key, obj[key]]) } } }
Object.keys
for (let i = 0; i < 10000; i++) { for (let keyArray = Object.keys(obj), i = keyArray.length - 1, key = keyArray[i]; i >= 0; key = keyArray[--i]) { console.log([key, obj[key]]); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.entries
for in
Object.keys
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 JSON represents a JavaScript microbenchmark test on MeasureThat.net, which compares the performance of three different approaches for iterating over an object: 1. `Object.entries` 2. `for...in` loop 3. `Object.keys` **Options Comparison** Here's a brief overview of each approach: * **`Object.entries`**: This method returns an array of two-element arrays, where each inner array contains the key-value pairs of the object as strings. + Pros: Efficient for objects with few properties, easy to read and understand. + Cons: May be slower than other approaches for large objects due to the overhead of creating the array. * **`for...in` loop**: This loop iterates over the object's own enumerable properties (key-value pairs). + Pros: Fast and efficient for iterating over properties, works well with inheritance. + Cons: Can be slower for very large objects due to the need to check `hasOwnProperty()` for each property. * **`Object.keys`**: This method returns an array of strings representing the object's own enumerable property names. + Pros: Fast and efficient for iterating over property names, easy to use with modern JavaScript features. + Cons: May not work as expected if the object has inherited properties. **Library Usage** None of the benchmarked methods rely on external libraries. However, `Object.entries` was introduced in ECMAScript 2015 (ES6) and may require older browsers or environments that don't support this feature. **Special JS Feature/Syntax** * **`Object.entries`**: Introduced in ES6, this method is not supported in older browsers or environments. * **`for...in` loop**: This loop has been part of JavaScript since its inception and works across all platforms. **Other Alternatives** If you need to iterate over an object's properties, other approaches include: * Using `Array.prototype.forEach()` with a custom callback function * Utilizing libraries like Lodash or Underscore.js for more complex object manipulation tasks Keep in mind that the choice of iteration method depends on your specific use case, performance requirements, and compatibility considerations.
Related benchmarks:
For in vs For of
Object.keys vs Object.values
Object.entries vs Object.keys vs for...in
entries vs keys lookup
Object.keys vs Object.entries vs Object.values
Comments
Confirm delete:
Do you really want to delete benchmark?