Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.entries VS Object.keys VS Object.entries with for of
(version: 0)
Comparing performance of:
Object.entries + foreach vs Object.keys + foreach vs Object.entries + for of
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 + foreach
const newObj = {}; Object.keys(window.parentObj).forEach((k) => { newObj[k] = window.parentObj[k]; });
Object.keys + foreach
const newObj = {}; Object.entries(window.parentObj).forEach(([k, v]) => { newObj[k] = v; });
Object.entries + for of
const newObj = {}; for (const [k, v] of Object.entries(window.parentObj)) { newObj[k] = v; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.entries + foreach
Object.keys + foreach
Object.entries + for of
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):
I'll break down the benchmark and its test cases for you. **Benchmark Definition** The benchmark is designed to compare the performance of three different ways to iterate over an object in JavaScript: 1. `Object.keys()` with `forEach` 2. `Object.entries()` with `forEach` 3. `Object.entries()` with `for...of` The script preparation code creates a large object (`window.parentObj`) with 100 properties, each generated by a random string. **Options Compared** Here's what's being compared: * `Object.keys()` + `forEach`: Iterates over the object's keys using `Object.keys()`, which returns an array of strings. The `forEach` method is then used to iterate over this array. * `Object.entries()` + `forEach`: Iterates over the object's key-value pairs using `Object.entries()`, which returns an array of arrays containing the key and value. The `forEach` method is then used to iterate over this array. * `Object.entries()` + `for...of`: Similar to the previous case, but uses a `for...of` loop to iterate over the object's key-value pairs. **Pros and Cons** Here are some pros and cons of each approach: * `Object.keys() + forEach`: + Pros: Simple and easy to read. Works well for objects with only string keys. + Cons: Can be slower than other methods, especially for large objects, since it requires iterating over an array. * `Object.entries() + forEach`: + Pros: More efficient than `Object.keys() + forEach`, especially for large objects, since it avoids creating an array. + Cons: Requires modern JavaScript features (ECMAScript 2015 and later) to work with the spread operator (`[...array]`) used in the benchmark. * `Object.entries() + for...of`: + Pros: Similar performance to `Object.entries() + forEach`, but more concise and readable. + Cons: Requires modern JavaScript features (ECMAScript 2015 and later) to work with the spread operator (`[...array]`) used in the benchmark. **Libraries Used** None. **Special JS Features/Syntax** The benchmark uses several modern JavaScript features: * ES6 spread operator (`[...]`) * `for...of` loop * Modern JavaScript syntax (e.g., template literals, arrow functions) These features are not supported in older versions of JavaScript and require at least ECMAScript 2015 to work. **Alternatives** Other alternatives for iterating over objects include: * Using a traditional `for` loop: `for (var key in obj) { ... }` * Using a library like Lodash, which provides a `keys()` method for iterating over object keys. * Using a data structure like an array or a Map to store the object's values.
Related benchmarks:
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 Array
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?