var data = { Array.from(Array(10000).keys()) };
Object.fromEntries(Object.entries(data));
Object.entries(data).reduce((acc, [k, v]) => {
acc[k] = v.toString();
return acc;
}, {});
Object.entries(data).reduce((acc, [k, v]) => ({
acc,
[k]: v.toString()
}), {});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.fromEntries | |
Reduce (reuse object) | |
Reduce (creating temporary objects) |
Test name | Executions per second |
---|---|
Object.fromEntries | 1510.8 Ops/sec |
Reduce (reuse object) | 885.4 Ops/sec |
Reduce (creating temporary objects) | 0.2 Ops/sec |
I'll break down the benchmark for you.
Benchmark Overview
The benchmark measures the performance of two approaches to create an object from an array of key-value pairs:
Object.fromEntries
: A built-in JavaScript method that creates an object from an array of [key, value] pairs.Reduce
with different variations:Options Compared
The benchmark compares the performance of three options:
Object.fromEntries
: A simple, built-in method that creates an object from an array of key-value pairs.Pros and Cons
Object.fromEntries
:Library
There is no specific library used in this benchmark. The Object.fromEntries
method is a built-in JavaScript feature introduced in ECMAScript 2015 (ES6). The reduce function is also a built-in JavaScript method.
Special JS Feature or Syntax
The benchmark uses the reduce
method, which is a functional programming technique that allows you to iterate over an array and accumulate values into a single object. This syntax is specific to JavaScript and is not directly related to any particular library or framework.
Other Alternatives
If you're interested in exploring alternative approaches for creating objects from arrays of key-value pairs, some options include:
forEach
method with an arrow function to iterate over the array and create an object.Keep in mind that these alternatives may not be as efficient or readable as the built-in methods used in this benchmark.