Spread it and Assign Again
Date tested:
one year ago
User agent:
Mozilla/5.0 (Linux; Android 12; Pixel 4a (5G)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36
Test name
Executions per second
map
395337.7 Ops/sec
empty assign
360321.5 Ops/sec
data assign
520873.5 Ops/sec
in place mod
1620585.0 Ops/sec
Benchmark definition (click to collapse):
Script Preparation code:
var data = { 'one': [{ 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" } } } ], 'two': [{ foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { replace: "yes", bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } } ] };
Tests:
map
let f = { ...data, two: data.two.map((item) => { item.foo.replace == "yes" ? { ...item, something: { n: 234, b: 234 } } : item }), };
empty assign
let o = Object.assign({}, data, { two: data.two.map((item) => { item.foo.replace == "yes" ? { ...item, something: { n: 234, b: 234 } } : item }) })
data assign
let o = Object.assign(data, { two: data.two.map((item) => item.foo.replace == "yes" ? { ...item, something: { n: 234, b: 234 } } : item ) });
in place mod
let f = data.two.find((item) => item.foo.replace == "yes"); f.n = 234; f.b = 234; data
Open this result on MeasureThat.net