Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.entries vs Generator vs Array.push with For in
(version: 0)
Comparing performance of:
Object.entries vs Generator vs Array.push with For in
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = { ...Array.from(Array(10000).keys()) };
Tests:
Object.entries
Object.entries(data);
Generator
[ ...(function* () { for (const key in data) { yield [key, data[key]]; } })() ];
Array.push with For in
const result = []; for (const key in data) { result.push([key, data[key]]); } result;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.entries
Generator
Array.push with For in
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.entries
1342.6 Ops/sec
Generator
501.5 Ops/sec
Array.push with For in
686.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON benchmark definition and test cases. **Benchmark Definition:** The benchmark is designed to compare the performance of three different approaches: 1. `Object.entries(data)`: This method returns an array of key-value pairs from the given object, `data`. It's a built-in JavaScript method that creates an array of key-value pairs. 2. A generator function (using `function*`) that yields key-value pairs from the `data` object: `[key, data[key]]`. The `(function* () { ... })()` syntax defines a generator function, which is a special type of function that can be used to create iterators. 3. `Array.push` with a for loop (using `for (const key in data)`) that adds key-value pairs to an array, `result`: `[key, data[key]]`. This approach uses the `push` method to add elements to an array. **Options Compared:** The benchmark is comparing the performance of these three approaches: * `Object.entries(data)` and its equivalent * The generator function (`function*`) and its execution using `(function* () { ... })()` * `Array.push` with a for loop (`for (const key in data)`) and its execution **Pros and Cons:** Here's a brief overview of the pros and cons of each approach: 1. `Object.entries(data)`: * Pros: Efficient, concise, and easy to read. * Cons: May not be as efficient as other approaches if the object is very large or has many properties. 2. Generator Function (`function*`): * Pros: Flexible, lazy evaluation, and can be useful in certain scenarios where iteration is necessary. * Cons: May have higher overhead due to the use of a generator function and the `(function* ...)` syntax. 3. `Array.push` with For Loop: * Pros: Simple and familiar for those who are used to working with arrays. * Cons: Less efficient than other approaches, especially when dealing with large datasets. **Library:** There is no explicit library mentioned in the provided JSON benchmark definition. However, it's worth noting that some browsers may have additional libraries or extensions that can affect the performance of these benchmarks. **Special JS Features/Syntax:** The generator function (`function*`) uses a special syntax called "generators" or "iterators." Generators are functions that return an iterator object, which can be used to iterate over values. This syntax is part of ECMAScript 2015 (ES6) and later versions. **Other Alternatives:** If you'd like to explore alternative approaches or modify the existing benchmarks, here are some options: * `Array.from(data)` instead of `Object.entries(data)` * Using a different array method, such as `map()` or `forEach()` * Implementing a custom iteration mechanism using a loop and a variable to keep track of the current index * Using Web Workers or Web Assembly for parallelization or acceleration
Related benchmarks:
Object.fromEntries vs create temp object
Array.forEach vs Object.keys().forEach
Map convert
Object.entries vs Generator
Comments
Confirm delete:
Do you really want to delete benchmark?