Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.entries VS Object.keys with obj[key]
(version: 0)
Comparing performance of:
Object.keys with obj[key] vs Object.entries
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var obj = {} for (const key of 'abcdefghijklmnopqrstuvwxyz'.split()) obj[key] = Math.random()
Tests:
Object.keys with obj[key]
Object.keys(obj).map((key) => [key, obj[key]])
Object.entries
Object.entries(obj).map(([key, value]) => [key, value])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.keys with obj[key]
Object.entries
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36 Edg/145.0.0.0
Browser/OS:
Chrome 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.keys with obj[key]
54477340.0 Ops/sec
Object.entries
44674416.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its findings. **What is being tested?** The provided benchmark measures the performance of two different approaches for iterating over an object in JavaScript: `Object.keys` with the "with obj[key]" syntax, and `Object.entries`. The test creates a large object `obj` with random key-value pairs using the script preparation code, and then uses both `Object.keys` and `Object.entries` to map each key-value pair. **Options compared** There are two options being compared: 1. **Object.keys with "with obj[key]" syntax**: This approach iterates over the object's keys in a specific order (alphabetical), and for each key, it accesses the corresponding value using the "with obj[key]" syntax. 2. **Object.entries**: This approach returns an array of arrays, where each inner array contains a key-value pair from the original object. **Pros and cons** * **Object.keys with "with obj[key]" syntax**: + Pros: Can be more memory-efficient since it doesn't require extra memory for the entire array of key-value pairs. + Cons: The iteration order is not guaranteed, which can lead to inconsistent results. Additionally, accessing the value using the "with obj[key]" syntax involves an additional lookup, which may incur a small performance penalty. * **Object.entries**: + Pros: Guarantees a consistent iteration order and can be more performant since it avoids the overhead of the "with obj[key]" syntax. + Cons: Requires extra memory to store the entire array of key-value pairs. **Library and purpose** In this benchmark, there is no external library used. The `Object.keys` and `Object.entries` methods are built-in JavaScript functions that operate on objects. **Special JS feature or syntax** There is no special JavaScript feature or syntax being tested in this benchmark. It only relies on standard JavaScript functionality. **Benchmark preparation code and result** The script preparation code creates a large object `obj` with random key-value pairs using the `for...of` loop, which is a modern JavaScript feature that allows iterating over iterable objects like strings. The HTML preparation code is empty. The benchmark results show the execution per second for each test case on a specific browser and device platform: Firefox 120 on a Macintosh (Intel Mac OS X 10.15). **Other alternatives** If you're interested in exploring alternative approaches, here are some options: * **Using `for...in` loop**: This approach iterates over the object's own enumerable properties, which may include inherited properties. * **Using `map()` function with a custom callback**: This approach can be more flexible since it allows using a custom callback function to transform each key-value pair. * **Using libraries like Lodash or Ramda**: These libraries provide additional functionality for working with objects and arrays, but may incur performance overhead. Keep in mind that these alternatives may change the behavior of the benchmark significantly, so it's essential to consider the specific requirements and constraints of your use case when choosing an approach.
Related benchmarks:
Object.keys(obj)[0] vs for in
Object.entries vs Object.keys vs for...in
Object.keys(object).includes(key) vs key in object
For in vs Object.entries 2
Comments
Confirm delete:
Do you really want to delete benchmark?