var data = Array.from(Array(10000).keys()).map(i => ({id: i, value: `val_${i}`}));
Object.fromEntries(data.map(obj => [obj.id, obj]));
data.reduce((acc, obj) => {
acc[obj.id] = obj;
return acc;
}, {});
data.reduce((acc, obj) => ({
acc,
[obj.id]: obj
}), {});
new Map(data.map(obj => [obj.id, obj]));
const res = {};
data.forEach(obj => {
res[obj.id] = obj;
});
const res = {};
for (const obj of data) {
res[obj.id] = obj;
}
const res = {};
for (let i = 0; i < data.length; i++) {
const obj = data[i];
res[obj.id] = obj;
}
const res = {};
const length = data.length;
for (let i = 0; i < length; i++) {
const obj = data[i];
res[obj.id] = obj;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.fromEntries | |
Reduce (reuse object) | |
Reduce (creating temporary objects) | |
Map | |
forEach | |
for of | |
for i | |
for i saved length variable |
Test name | Executions per second |
---|---|
Object.fromEntries | 2715.7 Ops/sec |
Reduce (reuse object) | 18107.6 Ops/sec |
Reduce (creating temporary objects) | 121.1 Ops/sec |
Map | 2077.8 Ops/sec |
forEach | 18834.5 Ops/sec |
for of | 18485.0 Ops/sec |
for i | 1461.3 Ops/sec |
for i saved length variable | 2625.2 Ops/sec |
Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark measures the performance of different approaches for converting an array to an object.
Benchmark Definition
The benchmark definition consists of a JSON object with several properties:
Name
: The name of the benchmark, which is "Convert Array to Object - Compare all possible".Description
: An empty string indicating that there is no description provided.Script Preparation Code
: A JavaScript code snippet that prepares an array of 10,000 objects, each with an id
and a value
.Html Preparation Code
: An empty string indicating that no HTML preparation code is required.Individual Test Cases
The benchmark consists of six individual test cases, each representing a different approach for converting the array to an object:
Object.fromEntries
method to create an object from an array of key-value pairs.reduce
method with an accumulator object and pushes new properties onto it, overwriting existing ones.Map
constructor to create a map from the array of key-value pairs.forEach
and assigns each element to a property on an empty object.for...of
and assigns each element to a property on an empty object.for
with an index variable and assigns each element to a property on an empty object, saving the length of the array as a variable.Library and Special Features
The benchmark uses the following libraries:
Array.from()
: Creates an array from an iterable.Object.fromEntries()
: A modern method for creating an object from key-value pairs.Map()
: Creates a map from key-value pairs.There are no special features used in this benchmark beyond the standard JavaScript APIs.
Performance Results
The performance results show the execution time for each test case on a Chrome 121 browser running on a Mac with an Intel processor. The results are:
The results indicate that the Reduce (reuse object)
approach is the fastest, followed closely by the Object.fromEntries
method. The other approaches are significantly slower, with the forEach
and for of
iterations being the slowest.
In conclusion, this benchmark demonstrates the performance differences between various approaches for converting an array to an object in JavaScript.