var obj = new Object()
var keys = (new Array(10000)).fill(0).map((x, i) => { return i + 1 })
keys.forEach((x) => { obj['prop' + x] = x })
for (var key in obj) {
console.log(obj[key]);
}
for(let value of Object.values(obj)){
console.log(value);
}
Object.keys(obj).forEach(key => console.log(obj[key]));
Object.entries(obj).forEach(([key, value]) => console.log(key,'->',value));
Object.values(obj).forEach(value => console.log(value));
let entries = Object.entries(obj)
let length = entries.length
for(let i = 0; i < length; i++){
console.log(entries[i][1]);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
For In | |
Object values | |
Object keys forEach | |
Object entries forEach | |
object values forEach | |
Object entries plus for loop |
Test name | Executions per second |
---|---|
For In | 41.1 Ops/sec |
Object values | 41.4 Ops/sec |
Object keys forEach | 42.3 Ops/sec |
Object entries forEach | 32.2 Ops/sec |
object values forEach | 40.1 Ops/sec |
Object entries plus for loop | 37.0 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark test case, which compares the performance of different approaches for iterating over an object in a loop.
Benchmark Definition
The benchmark definition defines two main variables:
obj
: an empty object created using the Object()
constructor.keys
: an array of 10,000 numbers generated using the fill()
and map()
methods.The script preparation code initializes the obj
variable and populates it with properties using the forEach()
method. The keys
array is then populated with values from 1 to 10,000.
Comparison Options
The benchmark test case compares six different approaches for iterating over the object in a loop:
for...in
loop syntax to iterate over the object's properties.Object.values()
method to get an array of the object's values.Object.keys()
method and the forEach()
method to iterate over the object's keys.Object.entries()
method and the forEach()
method to iterate over the object's key-value pairs.Object.values()
method and the forEach()
method to iterate over the object's values.Object.entries()
method to get an array of the object's key-value pairs, and then iterates over the array using a traditional for
loop.Pros and Cons
Here are some pros and cons for each approach:
for...in
.Object.values()
with added iteration logic.Library Use
The benchmark test case uses the following libraries:
Array.prototype.fill()
: fills an array with a specified value.Array.prototype.map()
: applies a function to each element of an array.Object.keys()
: returns an array of the object's keys.Object.values()
: returns an array of the object's values.Object.entries()
: returns an array of the object's key-value pairs.Special JavaScript Features or Syntax
None of the benchmark test cases use special JavaScript features or syntax that are not widely supported.
Alternative Approaches
Other approaches for iterating over objects in a loop include:
for...of
loops with Object.entries()
or Object.values()
.for
loops and accessing properties directly using bracket notation.forEach()
method on arrays of keys or values.These alternative approaches may offer different trade-offs in terms of performance, conciseness, and readability, depending on the specific use case and requirements.