Copying data; mapping vs. spreading then modifying vs. Object.assign then modifiying
Date tested:
one year ago
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:101.0) Gecko/20100101 Firefox/101.0
Test name
Executions per second
Mapping
16935210.0 Ops/sec
spreading, then modifying
8021250.0 Ops/sec
Object.assign, then modifying
1541070.0 Ops/sec
Benchmark definition (click to collapse):
Script Preparation code:
var data = [{ foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } } ];
Tests:
Mapping
data.map((d, i) => i=== 2 ? "replaced" : d)
spreading, then modifying
const copy = [...data]; copy[2] = "replaced";
Object.assign, then modifying
const copy = Object.assign([], data) copy[2] = "replaced";
Open this result on MeasureThat.net