Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.entries VS Object.keys
(version: 0)
Comparing performance of:
Object.entries vs Object.keys
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]; } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.entries
Object.keys
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.entries
56386.9 Ops/sec
Object.keys
71640.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the explanation into smaller sections. **Benchmark Definition JSON** The provided JSON represents a JavaScript benchmark test case on MeasureThat.net. Here's what's being tested: * Two different approaches are compared: + `Object.entries`: This method returns an array of a given object's key-value pairs, where each pair is an array with the key and value. + `Object.keys`: This method returns an array of a given object's keys (without values). * The purpose of the benchmark is to compare the performance of these two methods when iterating over an object and adding only every other key-value pair to a new object. **Approaches Comparison** There are two main approaches: 1. **Object.entries**: This method returns an array of key-value pairs, where each pair is an array with the key and value. When iterating over this array using `forEach`, it's necessary to access both elements of the pair (key and value) on every iteration. 2. **Object.keys**: This method returns an array of keys without values. When iterating over this array using `forEach`, it's only necessary to access each key once. **Pros and Cons** Here are some pros and cons of each approach: 1. **Object.entries** * Pros: + More intuitive when working with objects, as it returns the key-value pairs directly. + Can be more readable in code when iterating over the object. * Cons: + Requires accessing both key and value on every iteration, which can lead to slower performance. 2. **Object.keys** * Pros: + Faster performance because only keys are accessed on each iteration. * Cons: + May be less intuitive when working with objects, as it returns only the keys. **Library and Purpose** In this benchmark, no specific library is used. The `window.parentObj` object is created manually to simulate a real-world scenario where an object needs to be iterated over. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes being tested in this benchmark. However, the use of `forEach` loop and arrow functions (`=>`) is a modern JavaScript feature that's commonly used. **Other Alternatives** If you need to iterate over an object and add only every other key-value pair to a new object, you can also consider using: 1. **For...in loops**: An older JavaScript method for iterating over objects, but it's still supported in most browsers. 2. **Array.prototype.slice()**: You can use `slice()` to create a subset of the array returned by `Object.entries` or `Object.keys`, and then iterate over the resulting array. Keep in mind that performance differences between these approaches are usually small and may not be noticeable in most cases. However, if you're working with large datasets or performance-critical code, using optimized methods like `Object.entries` with a loop can provide better results.
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
Object.key vs Object.value vs Object.entries
Array of key values - Object.entries VS Object.keys
Object.entries VS Object.keys VS Object.values
Object entry counting: Object.entries VS Object.keys VS Object.values
Comments
Confirm delete:
Do you really want to delete benchmark?