Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for let vs Object.keys 2
(version: 0)
Comparing performance of:
for let i in vs Object.keys (cached) vs Object.keys
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var myObject = {}; var sum = 0; for (let i = 0; i < 1000; i++) myObject["property_" + Math.random()] = Math.random(); var keys = Object.keys(myObject);
Tests:
for let i in
for (let i in myObject) sum += myObject[i];
Object.keys (cached)
for (let i = 0 ; i < keys.length ; i++) sum += myObject[keys[i]];
Object.keys
const newKeys = Object.keys (myObject); for (let i = 0 ; i < newKeys.length ; i++) sum += myObject[newKeys[i]];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for let i in
Object.keys (cached)
Object.keys
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/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for let i in
6014.1 Ops/sec
Object.keys (cached)
4783.5 Ops/sec
Object.keys
6167.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the JavaScript microbenchmark on MeasureThat.net. The provided benchmark compares three approaches to iterate over an object in JavaScript: 1. **For-in loop**: The first approach uses a traditional `for` loop with `in` keyword, which iterates over the object's own enumerable properties. ```javascript for (let i in myObject) { sum += myObject[i]; } ``` Pros: Simple and straightforward. Cons: Can be slow for large objects, as it uses a lot of overhead to iterate over the object's properties. Additionally, it can also include non-enumerable properties, which might not be what you want. 2. **Array iteration**: The second approach uses an array of the object's keys, accessed through `Object.keys()` or a cached version. ```javascript const newKeys = Object.keys(myObject); for (let i = 0; i < newKeys.length; i++) { sum += myObject[newKeys[i]]; } ``` Pros: Faster than the for-in loop, especially for large objects. This is because array iteration is more efficient and doesn't include non-enumerable properties. Cons: Requires an additional step to get the array of keys, which can add overhead. 3. **Cached Object.keys()**: The third approach uses a cached version of `Object.keys()` that returns a temporary array, rather than creating a new one each time. ```javascript const newKeys = Object.keys(myObject); for (let i = 0; i < newKeys.length; i++) { sum += myObject[newKeys[i]]; } ``` Pros: Similar to the second approach, but with less overhead since the cache is reused. Cons: Requires a temporary array allocation, which can add some overhead. Now, let's look at the specific libraries used in this benchmark: * None. This benchmark only uses built-in JavaScript features and doesn't rely on any external libraries. As for special JavaScript features or syntax, the benchmark uses: * **Template literals**: Used in the `for-in` loop to create a string with dynamic property names. * **Arrow functions**: Not used explicitly, but implicitly through the use of `const newKeys = Object.keys(myObject);`. Other alternatives that could be considered for this benchmark include: * Using `for...of` loop instead of `for...in`, which is a more modern and efficient way to iterate over arrays. * Using `map()` or `reduce()` methods, which can provide a more concise and expressive way to perform operations on arrays. * Using a library like Lodash, which provides utility functions for working with objects and arrays. However, the choice of iteration method ultimately depends on the specific requirements and constraints of the project.
Related benchmarks:
keys vs values
Map vs Object with Number Keys
Object creation from property array, manual vs Object.fromEntries
for let vs Object.keys 3
Comments
Confirm delete:
Do you really want to delete benchmark?