Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.keys + map vs for...in + push
(version: 0)
Comparing performance of:
Object.keys + map vs for...in + push
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var data = [ { "SDGHDJJ": "FDSFSDF", "FDSDS": "GFDGFDG", "5SDFFD": "GFDGDFG", "353G": "THFGJGFGF", "6BDFSD": "GFDDWERT", "53523FV": "HGFHGFHFGHF", "FSDGSDGS4": "FDSFDTYF", "532523GSV": "SADAHHDFG", "253GSG": "GFDGDFHDF", "ADFGJKU5": "GDFJKTRRW", "354GFG": "DSFSDHRRR" } ]; var result = [];
Tests:
Object.keys + map
result = Object.keys(data[0]).map(prop => ( { wch: Math.max(prop.toString().length, ...data.map(obj => obj[prop] ? obj[prop].toString().length : 0)) } ));
for...in + push
for (const prop in data[0]) { const maxWidth = Math.max(prop.toString().length, ...data.map(obj => obj[prop] ? obj[prop].toString().length : 0)); result.push({ wch: maxWidth }); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.keys + map
for...in + push
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.keys + map
367440.6 Ops/sec
for...in + push
230141.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark is comparing two approaches to find the maximum length of a string within an array of objects: 1. **Object.keys + map**: This approach uses `Object.keys()` to get an array of property names from the first object in the `data` array, and then uses `map()` to create a new array with the maximum length of each property name. 2. **for...in + push**: This approach uses a traditional `for...in` loop to iterate over the properties of the first object in the `data` array, and then pushes the maximum length of each property name into an array called `result`. **Comparison** The two approaches are being compared to determine which one is faster. The benchmark will run both approaches multiple times and report the average execution time for each. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Object.keys + map**: + Pros: More concise, readable, and expressive code. Uses modern JavaScript features like `map()`. + Cons: May be slower due to the overhead of function calls and memory allocation. * **for...in + push**: + Pros: Can be faster for large arrays since it avoids the overhead of function calls and memory allocation. + Cons: Code is more verbose, less readable, and less expressive. **Library** There is no explicit library mentioned in the JSON. However, `Object.keys()` is a built-in JavaScript method that returns an array of property names from an object. **Special JS Feature/Syntax** The benchmark uses modern JavaScript features like `map()`, which is supported by most modern browsers and Node.js environments. There are no special JS features or syntax mentioned in the code. **Alternatives** If you want to experiment with alternative approaches, here are some options: * **Lodash**: A popular utility library that provides a `maxBy()` function, similar to `map()`. This could potentially be faster than `Object.keys + map` since it's optimized for performance. * **Array.prototype.reduce()**: An alternative way to find the maximum value in an array. This approach is more concise than `for...in + push`, but may not be as efficient due to the overhead of function calls and memory allocation. * **Native Array.prototype.map() with a custom callback**: You can use `map()` with a custom callback function that returns the length of each property name, similar to `Object.keys + map`. This approach avoids the overhead of `Object.keys()` but may not be as readable or concise.
Related benchmarks:
Test of 2
entries vs keys lookup
checks if object has any key - Object.keys vs for key in 2
Map convert
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?