Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.entries VS Object.keys VS Object.keys with extra array
(version: 0)
Comparing performance of:
Object.entries vs Object.keys vs Object.keys with extra array
Created:
7 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 = {}; for (let i = 0; i < 100; i++) { window.parentObj[makeid()] = 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; } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.entries
Object.keys
Object.keys with extra 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):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net, which compares the performance of three approaches for iterating over an object's keys: 1. `Object.entries()` 2. `Object.keys()` 3. `Object.keys()` with an additional array to extract both key and value. **Options Compared** The benchmark tests the following options: * `Object.entries()`: Returns an array of a given object's own enumerable string-keyed property [key, value] pairs. * `Object.keys()`: Returns an array of all the available keys for that object. * `Object.keys()` with extra array: This approach is similar to `Object.keys()`, but it also extracts the corresponding values using an additional array. **Pros and Cons** Here's a brief summary of each option: ### Object.entries() * Pros: + Returns both key and value in a single iteration, which can be more efficient for large objects. + Can be used to directly assign values to new objects without iterating over the keys separately. * Cons: + Requires 2 iterations (first to get keys, second to iterate over entries). + May have slower performance due to additional iteration. ### Object.keys() * Pros: + Faster than `Object.entries()` since it only iterates over keys once. + More straightforward and easier to understand for those familiar with the method. * Cons: + Returns only the keys, requiring an additional step to iterate over values. + May not be as efficient if the object has a large number of keys. ### Object.keys() with extra array * Pros: + Combines the benefits of both approaches by extracting key and value in a single iteration. + Can potentially improve performance since it avoids the need for additional iterations. * Cons: + Requires more complex logic to extract values, which can increase readability issues if not done correctly. **Library Usage** None of the benchmarked options use external libraries. The `window.parentObj` variable is used as a mock object, and the `makeid()` function is a custom utility for generating random IDs. **Special JS Features/Syntax** There are no special JavaScript features or syntaxes used in this benchmark. **Alternative Approaches** Other approaches to iterate over an object's keys might include: * Using a `for...in` loop * Utilizing a library like Lodash or Underscore.js for iteration and manipulation of objects * Implementing a custom iterator function using the `Symbol.iterator` method However, these alternatives are not tested in this specific benchmark.
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
Array of key values - Object.entries VS Object.keys
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?