var datas = [{ean:"123456789012", hash:"SNFOsdNSF12324FJSBFOnoinfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"SNFONSFfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"SNFONSF12324FJinfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"ONSF12324FJSBFOnoinfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"SNFONSF12324FJSBFOnoi", price:"43,7"},{ean:"123456789012", hash:"SNFONdfdfdf12324FJSBFOnoinfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"SNFONSF12324FJSBFOnoinfdoinfn22fdin", price:"43,7"},{ean:"123456789012", hash:"SNFONSF1232433FJSBFOnoinfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"SN44FONSF12324FJSBFOnoinfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"SNFONSF12324FJSBFOnoinfdoi55nfnfdin", price:"43,7"},{ean:"123456789012", hash:"SN66FONSF12324FJSBFOnoinfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"77SNFONSF12324FJSBFOnoinfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"SNFONSF12324FJSBFOnoinfdoinfn88fdin", price:"43,7"},{ean:"123456789012", hash:"SNFONSF9912324FJSBFOnoinfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"SNFONSF12324FJSB000FOnoinfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"SNFONSF12324FJSBFOnoi2334nfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"SNFONSF123267778JSBFOnoinfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"S9ONSF12324FJSBFOnoinfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"SNFON0000TRTR24FJSBFOnoinfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"SNFONSF1232YUYU4FJSBFOnoinfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"SNFOYNSF12324FJSBFOnoinfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"SNIOIFONSF12324FJSBFOnoinfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"SNFONSF12324FJSBFOnoinIfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"SNFONSF12324F2P2JSBFOnoinfdoinfnfdin", price:"43,7"},{ean:"123456789012", hash:"SNFONSF12324FJSBFOnoinfdoinf44nfBBBdin", price:"43,7"},{ean:"123456789012", hash:"SNFVVVONSF12324FJSB333FOnoinfdoinfnfdin", price:"43,7"}]
var array = [];
for(var i = 0; i < datas.length; i++){
array[datas[i].hash] = datas[i].price;
}
array = datas.reduce(function(a, product){
a[product.hash] = product.price;
return a;
}, {});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for | |
reduce |
Test name | Executions per second |
---|---|
for | 100468.3 Ops/sec |
reduce | 325841.8 Ops/sec |
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark.
Benchmark Purpose
The purpose of this benchmark is to compare two approaches for populating an object array from an array of data objects: the for
loop and the reduce()
method. The test case uses a sample dataset with 23 entries, each containing an "ean" ( presumably an identifier), a "hash", and a "price".
Options Being Compared
The two options being compared are:
for
loop to iterate through the data array and populate the object array.reduce()
method, which applies a callback function to each element in the array, accumulating a result.Pros and Cons
reduce()
method.Library and Purpose
The reduce()
method is a built-in JavaScript function that applies a callback function to each element in an array, accumulating a result. In this benchmark, it's used to populate the object array by iterating through the data array and updating the corresponding values in the object array.
Special JS Feature or Syntax
There are no special JavaScript features or syntaxes being tested in this benchmark. However, it's worth noting that the reduce()
method is a modern JavaScript feature introduced in ECMAScript 2015 (ES6).
Other Alternatives
If you're interested in exploring alternative approaches to populating an object array from an array of data objects, some other options include:
forEach()
: This method is similar to for
loop but provides additional benefits like support for generators and async iterations.Array.prototype.map()
): This approach can be more concise than using reduce()
or a for
loop, but may not be as efficient.mapValues()
function): These libraries provide optimized and tested implementations for common array processing tasks.I hope this explanation helps you understand the benchmark being tested on MeasureThat.net!