var items = Array.from(Array(1000), (_, x) => ({
key: x,
value1: Math.random() * 1000,
value2: Math.random() * 1000
}));
var objContainer = {};
var arrContainer = [];
for (let i = 100; i >= 0; i--) {
const index = Math.floor(Math.random() * 1000);
const item = items[index];
objContainer[item.key] = item;
arrContainer.push(item)
}
var result = [];
for (var k in objContainer) {
if (objContainer[k].value1 >= 0 && objContainer[k].value1 <= 499 && objContainer[k].value2 >= 500 && objContainer[k].value2 <= 999) {
result.push(k);
}
}
var result = [];
for (var item of arrContainer) {
if (item.value1 >= 0 && item.value1 <= 499 && item.value2 >= 500 && item.value2 <= 999) {
result.push(item.key);
}
}
var result = [];
var keys = Object.keys(objContainer);
for (var i = 0; i < keys.length; i++) {
if (objContainer[keys[i]].value1 >= 0 && objContainer[keys[i]].value1 <= 499 && objContainer[keys[i]].value2 >= 500 && objContainer[keys[i]].value2 <= 999) {
result.push(keys[i]);
}
}
var result = [];
for (var i = 0; i < arrContainer.length; i++) {
if (arrContainer[i].value1 >= 0 && arrContainer[i].value1 <= 499 && arrContainer[i].value2 >= 500 && arrContainer[i].value2 <= 999) {
result.push(arrContainer[i].key);
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object, for-in | |
Array, for-of | |
Object, for | |
Array, for |
Test name | Executions per second |
---|---|
Object, for-in | 65288.6 Ops/sec |
Array, for-of | 4189983.0 Ops/sec |
Object, for | 63273.3 Ops/sec |
Array, for | 61496.4 Ops/sec |
Let's break down the provided benchmark and explain what is being tested.
Benchmark Definition
The benchmark tests the speed of three different approaches to compare object values:
for...in
loop to iterate over the object's properties (keys) and checking if a property value falls within a certain range.for...of
loop to iterate over an array of objects and extracting the key from each object using the key
property.for
loop to iterate over an array of keys and checking if each key's corresponding object value falls within the specified range.for
loop to iterate over an array of objects and extracting the key from each object using the key
property.Comparison
The benchmark measures the execution speed of these four approaches on both object and array data structures. The results are presented in terms of "ExecutionsPerSecond", which indicates how many iterations (i.e., comparisons) can be performed within a second by each browser.
Options Compared
Here's a brief summary of each option:
for...in
, which has the advantage of being concise but may also incur performance overhead due to property lookup.for...of
) to iterate over an array of objects. This approach is generally faster than traditional loops because it avoids unnecessary object creation and property access.for
loop, which can be less readable but may also perform better due to potential optimizations.for
loop to iterate over an array of objects, similar to the object iteration approach.Pros and Cons
Here's a brief analysis of each approach:
for...of
loop syntax in the browser engine.Other Considerations
Alternatives
If you're interested in exploring other approaches or optimizations for object comparison, consider:
Map
data structures instead of objects or arrays, as they provide more efficient key-value lookups.WeakMap
or Map.prototype.has()
to avoid unnecessary property checks.Keep in mind that these alternatives might require additional setup and code modifications to work within your specific use case.