Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object mapping: Object.entries VS Object.keys VS Object.keys with extra array VS Object.entries without array
(version: 0)
Comparing performance of:
Object.entries vs Object.keys vs Object.keys with extra array vs Object.entries without array
Created:
3 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()] = { innerVal: makeid() }; }
Tests:
Object.entries
const newObj = {}; Object.entries(window.parentObj).forEach(([k, v]) => { newObj[k] = v; });
Object.keys
const newObj = {}; Object.keys(window.parentObj).forEach((k, i) => { 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]] newObj[extraK] = v; });
Object.entries without array
const newObj = {}; Object.entries(window.parentObj).forEach((keyAndVal, i) => { newObj[keyAndVal[0]] = keyAndVal[1]; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Object.entries
Object.keys
Object.keys with extra array
Object.entries without 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):
Let's dive into the benchmark analysis. **What is tested:** The provided JSON represents a JavaScript microbenchmark test case that compares the performance of different methods for mapping over an object: 1. `Object.entries`: Returns an array of key-value pairs from the object, where each pair is in the format `[key, value]`. 2. `Object.keys`: Returns an array of keys from the object. 3. `Object.keys` with an extra array: This method returns two arrays, one for keys and another for values, which are then concatenated to create a single object. 4. `Object.entries` without array: This method is similar to `Object.entries`, but it doesn't return an array. **Options compared:** The benchmark compares the performance of these four methods in terms of execution speed (measured as executions per second). **Pros and Cons:** 1. **Object.entries**: Pros: * Efficient way to iterate over key-value pairs. * Returns an array, which can be used for further processing. Cons: * May consume more memory, especially for large objects. 2. **Object.keys**: Pros: * Fast and lightweight. Cons: * Only returns keys, not values. 3. **Object.keys with extra array**: Pros: * Combines the benefits of both methods (keys and values). Cons: * Returns two arrays, which can increase memory usage. 4. **Object.entries without array**: This method is similar to `Object.entries`, but it doesn't return an array. **Library and purpose:** There is no explicit library mentioned in the provided JSON. However, `Object.entries` and `Object.keys` are built-in JavaScript methods that have been available since ECMAScript 2015 (ES6). **Special JS feature or syntax:** There is a special feature used in this benchmark: * The `forEach` method with an arrow function (`(k, i) => { ... }`) is used to iterate over the keys and values returned by `Object.keys`. This is a concise way to perform iteration and access both the key and value. * The spread operator (`[k, window.parentObj[k]]`) is used in the "Object.keys with extra array" method to create a new array for values. **Other alternatives:** If you don't want to use `forEach` or arrow functions, you can achieve similar results using traditional `for...in` loops and accessing the key-value pairs manually. However, this approach would likely be less efficient due to its verbosity. For an alternative approach, you could use libraries like Lodash (specifically, the `map` function) or Ramda (specifically, the `over` function), which provide functional programming utilities for iteration and transformation of data.
Related benchmarks:
Object.keys vs Object.values
Object keys vs Array map v2
keys vs values
entries vs keys lookup
Object.keys vs Object.entries vs Object.values
Comments
Confirm delete:
Do you really want to delete benchmark?