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 2
(version: 0)
Comparing performance of:
Object.entries vs Object.entries.for
Created:
4 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.entries.for
const newObj = {}; for (const [k, v] of Object.entries(window.parentObj)) { if ((i % 2) === 0) { newObj[k] = window.parentObj[k]; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.entries
Object.entries.for
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 definition and test cases. **What is being tested?** The benchmark tests three approaches for iterating over an object in JavaScript: 1. **`Object.entries()`**: This method returns an array of key-value pairs from the object, where each pair is an array with two elements: the key and the value. 2. **`Object.keys()`**: This method returns an array of keys from the object. 3. **`Object.keys()`** with an additional `extra array`: This approach is not explicitly defined in the benchmark definition, but it's likely referring to using `Object.keys()` followed by some other operation (e.g., pushing elements onto an array). 4. **`Object.entries()` without an array**: Similar to the previous point, this approach is not clearly defined, but it might be a misunderstanding of how `Object.entries()` works. **Options compared** The benchmark compares the performance of these four approaches: * Iterating over an object using `Object.entries()` (with and without an additional array) * Iterating over an object using `Object.keys()` * Using `Object.keys()` followed by some other operation ( likely just pushing elements onto an array, but not explicitly defined) **Pros and cons of each approach** 1. **`Object.entries()`**: This method is designed specifically for iterating over objects, making it a good choice. However, if the object has only one key-value pair, `Object.keys()` might be faster (since it doesn't create an extra array). 2. **`Object.keys()`**: This method can be slower than `Object.entries()` because it creates an array of keys and then iterates over it, even though the iteration is not necessary. 3. **`Object.keys()`** with an additional array: This approach seems unnecessary and might even introduce overhead (e.g., creating a new array). 4. **`Object.entries()` without an array**: Again, this approach seems to be a misunderstanding of how `Object.entries()` works. If you want to iterate over an object, using `Object.entries()` with an additional operation (like pushing elements onto an array) might not be the best choice. **Libraries used** None are explicitly mentioned in the benchmark definition, but we can make some educated guesses: * `window.parentObj` is likely a global variable that stores the parent object for the test case. This suggests that the benchmark uses some kind of testing framework or library to manage the objects and their properties. * `makeid()` might be a utility function used to generate random keys for the objects. **Special JS features or syntax** There doesn't seem to be any special JavaScript features or syntax being tested in this benchmark. The code is written in vanilla JavaScript, without any modern features like async/await, Promises, or ES6+ modules. **Alternatives** To create a similar benchmark, you could consider using other methods for iterating over objects, such as: * Using `for...in` to iterate over the object's properties * Using `forEach()` with an arrow function * Using `reduce()` to iterate over the object's key-value pairs Keep in mind that these alternatives might have different performance characteristics and use cases compared to the methods tested in this 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?