Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.entries VS Object.keys VS Object.keys with extra array VS Object.entries without array VS Array
(version: 0)
Comparing performance of:
Object.entries vs Object.keys vs Object.keys with extra array vs Object.entries without array vs array
Created:
3 years ago
by:
Guest
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 = {}; window.parentObjArr = []; for (let i = 0; i < 100; i++) { window.parentObj[makeid()] = makeid(); parentObjArr.push(makeid()); }
Tests:
Object.entries
const newObj = {}; Object.entries(window.parentObj).forEach(([k, v], i) => { if ((i % 2) === 0) { newObj[k] = v; } });
Object.keys
const newObj = {}; Object.keys(window.parentObj).forEach((k, i) => { if ((i % 2) === 0) { newObj[k] = window.parentObj[k]; } });
Object.keys with extra array
const newObj = {}; Object.keys(window.parentObj).forEach((k, i) => { const [extraK, v] = [k, window.parentObj[k]] if ((i % 2) === 0) { newObj[extraK] = v; } });
Object.entries without array
const newObj = {}; Object.entries(window.parentObj).forEach((keyAndVal, i) => { if ((i % 2) === 0) { newObj[keyAndVal[0]] = keyAndVal[1]; } });
array
const newObj = {}; window.parentObjArr.forEach((keyAndVal, i) => { if ((i % 2) === 0) { newObj[keyAndVal[0]] = keyAndVal[1]; } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Object.entries
Object.keys
Object.keys with extra array
Object.entries without array
array
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):
Let's break down the provided benchmark and explain what's being tested. **Overview** The benchmark compares the performance of different approaches to iterate over object keys using `Object.keys()` and `Object.entries()`. The test cases are designed to create an array of objects with unique IDs, where each object has a single key-value pair. The iteration approach is varied to highlight differences in execution time and efficiency. **Test Cases** 1. **Object.entries**: Iterates over the object's own enumerable properties using `Object.entries()`, which returns an array of tuples containing each key-value pair. 2. **Object.keys**: Iterates over the object's own enumerable property names using `Object.keys()`. 3. **Object.keys with extra array**: Iterates over the object's own enumerable properties, but with a twist: it uses an array comprehension to extract both the key and value from each tuple returned by `Object.entries()`. 4. **Object.entries without array**: Similar to the previous test case, but without using an array comprehension. 5. **Array**: Iterates over an array of objects using `forEach()`. **Library and Syntax** The benchmark uses JavaScript's built-in `Object.keys()` and `Object.entries()` methods, which are part of the ECMAScript standard (ECMA-262). No external libraries or dependencies are required for this benchmark. **JavaScript Features** None of the test cases use any special JavaScript features like async/await, generators, or lambda functions. The code is straightforward and simple, making it accessible to a wide range of developers. **Performance Comparison** The benchmark measures execution time per second (ExecutionsPerSecond) for each test case, which indicates how many iterations can be performed within a given timeframe. The results show that: * `Object.keys` performs the best, followed closely by `Array`, indicating that iterating over object keys directly is relatively efficient. * `Object.entries without array` and `Object.entries with extra array` are slower than the previous two cases but still faster than each other. * `Object.entries` has a higher execution time compared to `Object.keys`, likely due to the additional overhead of creating an array from the iterator. **Alternatives** Some alternative approaches to iterating over object keys or arrays could be: 1. **Using a for...in loop**: This would iterate over both own and inherited properties, potentially affecting performance. 2. **Using a for...of loop**: Similar to `forEach()`, but with additional capabilities like automatic iteration and assignment of value to a variable. 3. **Utilizing a Map data structure**: Mapping objects can provide faster lookups and iteration, especially for large datasets. 4. **Employing custom iterator methods**: Creating a custom iterator class or using a library like `iterable-iterator` could offer more control over the iteration process. Keep in mind that the choice of iteration approach depends on the specific requirements of your project, including performance constraints, code readability, and maintainability considerations.
Related benchmarks:
Object values: Object.entries VS Object.keys VS Object.keys with extra array VS Object.entries without array
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 Object.keys as separate array with for loop
Comments
Confirm delete:
Do you really want to delete benchmark?