Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs Object.keys (10k items)
(version: 0)
Comparing performance of:
for-in vs Object.keys
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = {}; for (var i = 10; i < 10010; i++) { data[i.toString(36)] = i; }
Tests:
for-in
var total = 0; for (var key in data) { if(!Object.prototype.hasOwnProperty.call(data, key)) continue; total += data[key]; }
Object.keys
var total = 0; Object.keys(data).forEach(key => { total += data[key]; })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for-in
Object.keys
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 world of JavaScript microbenchmarks! **Benchmark Definition** The provided JSON represents a benchmark test for comparing the performance of two different ways to iterate over an object: `for-in` and `Object.keys`. The benchmark is designed to test how many times each loop can execute per second on a Mac OS X 10.15.7 machine with Chrome 112 browser. **Script Preparation Code** The script preparation code creates an empty object `data` and then populates it with 10,000 items using a for loop that converts each number to a base-36 string as the key. This ensures that the object has a large number of entries, making the benchmark more representative of real-world scenarios. **Html Preparation Code** There is no HTML preparation code provided, which means that the benchmark only tests the JavaScript execution time and does not take into account any factors related to HTML parsing or rendering. **Individual Test Cases** The two test cases are: 1. **for-in**: This loop iterates over the `data` object using the `for-in` syntax, which automatically includes inherited properties in the iteration. 2. **Object.keys**: This loop uses the `Object.keys()` method to get an array of the object's own enumerable property names, and then iterates over this array using the `forEach()` method. **Options Compared** The benchmark compares two options: 1. **for-in**: This approach has some advantages: * It can be more concise and easier to read than other approaches. * It is a widely supported syntax in JavaScript. * However, it has some disadvantages: + It includes inherited properties in the iteration, which may not be desirable in all cases. + Its performance can be slower due to the overhead of checking for own property existence using `Object.prototype.hasOwnProperty.call()`. 2. **Object.keys**: This approach has its own set of advantages and disadvantages: * It is more explicit and easier to understand than the `for-in` syntax. * It provides better control over which properties are included in the iteration. * However, it may have some performance overhead due to the creation of an array and the need to iterate over it. **Pros and Cons** Here's a summary of the pros and cons of each approach: * **for-in**: + Pros: concise, widely supported, easy to read + Cons: includes inherited properties, slower performance * **Object.keys**: + Pros: explicit, better control over iteration, faster performance (in some cases) + Cons: may have higher overhead due to array creation and iteration **Library and Special JS Feature** There is no library used in this benchmark. However, the `Object.prototype.hasOwnProperty.call()` method is a built-in JavaScript method that is used to check if an object has a specific property. **Other Alternatives** Some alternative approaches could be: * Using `Array.from()` and `reduce()` methods to iterate over the object's properties. * Using `for...of` loop, which was introduced in ECMAScript 2015 (ES6) as a more modern and explicit way of iterating over arrays and objects. * Using libraries like Lodash or Ramda to perform iteration and other utility operations. Keep in mind that each approach has its own trade-offs and performance characteristics, and the choice of which one to use depends on the specific requirements of your project.
Related benchmarks:
for vs. for-of vs. reduce (array to ID-keyed object)
Object iteration for-in vs object.keys
for-in vs object.keys vs object.values for objects perf 5
for in / object.keys test
Comments
Confirm delete:
Do you really want to delete benchmark?