spread vs for loop vs array push vs slice vs Array.from
Date tested:
3 years ago
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36
Test name
Executions per second
spread
8849440.0 Ops/sec
for loop
853478.4 Ops/sec
for loop (array push)
817239.6 Ops/sec
slice
9550151.0 Ops/sec
Array.from
3424189.0 Ops/sec
Benchmark definition (click to collapse):
Script Preparation code:
var obj = [ { type: "fixedCost", description: { content: "<p><br></p>", widgets: {}, tokens: [ { type: "block", subType: "paragraph", }, { type: "inline", subType: "text", content: "TMS and ESS Licences", style: {}, }, ], }, quantity: 1, discount: { enabled: false, type: "percent", units: 0, }, rate: { unit: "Employee Licence", rate: 4.05, }, isTaxExempt: false, interactive: { isOptional: false, isOptionalSelected: false, isQuantityOptional: false, quantityRange: { min: 0, max: 0, }, isOptionalQuantity: false, }, currency: "GBP", id: "k55w3lGzGM4", }, { type: "fixedCost", description: { content: "<p><br></p>", widgets: {}, tokens: [ { type: "block", subType: "paragraph", }, { type: "inline", subType: "text", content: "Biometric Clock", }, ], }, quantity: 5, discount: { enabled: false, type: "percent", units: 0, }, rate: { unit: "Unit", rate: 1500, }, isTaxExempt: false, interactive: { isOptional: true, isOptionalSelected: false, isQuantityOptional: true, quantityRange: { min: 0, max: 5, }, }, currency: "GBP", id: "iQWwrO6qLew", }, { type: "fixedCost", description: { content: "<p><br></p>", widgets: {}, tokens: [ { subType: "paragraph", type: "block", }, { content: "Proximity Clock", subType: "text", type: "inline", }, ], }, quantity: 5, discount: { enabled: false, type: "percent", units: 0, }, rate: { unit: "Unit", rate: 1500, }, isTaxExempt: false, interactive: { isOptional: true, isOptionalSelected: false, isQuantityOptional: true, quantityRange: { min: 0, max: 100, }, }, currency: "GBP", id: "u0eMdVcAx1U", }, { type: "text", description: { content: "<p><br></p>", widgets: {}, tokens: [ { type: "block", subType: "paragraph", }, { type: "inline", subType: "text", content: "T&A Clocking Terminals", style: {}, }, ], }, interactive: { isOptional: false, isOptionalSelected: false, }, id: "dPVSGJMm97g", }, { type: "fixedCost", description: { content: "<p><br></p>", widgets: {}, tokens: [ { type: "block", subType: "paragraph", }, { type: "inline", subType: "text", content: "MF900 Stainless Steel T&A/Muster Point (UK Only) (Reader Required - To be Identified from External Readers)", style: {}, }, ], }, quantity: 1, discount: { enabled: false, type: "percent", units: 0, }, rate: { unit: "Unit", rate: 1750, }, isTaxExempt: false, interactive: { isOptional: false, isOptionalSelected: false, isQuantityOptional: false, quantityRange: { min: 0, max: 0, }, isOptionalQuantity: false, }, currency: "GBP", id: "BCLCCimoZ6M", }, { type: "fixedCost", description: { content: "<p><br></p>", widgets: {}, tokens: [ { type: "block", subType: "paragraph", }, { type: "inline", subType: "text", content: "MF901 Stainless Steel T&A/Muster Point - PoE - (Beta Test Sites Only) (Reader Required - To be Identified from External Readers)", style: {}, }, ], }, quantity: 1, discount: { enabled: false, type: "percent", units: 0, }, rate: { unit: "Unit", rate: 1750, }, isTaxExempt: false, interactive: { isOptional: false, isOptionalSelected: false, isQuantityOptional: false, quantityRange: { min: 0, max: 0, }, isOptionalQuantity: false, }, currency: "GBP", id: "AOqDHpIIeAI", }, ];
Tests:
spread
let newObj = [...obj]
for loop
let newObj = [] for(let i=0; i<obj.length; i++) { newObj[i] = obj[i] }
for loop (array push)
let newObj = [] for(let i=0; i<obj.length; i++) { newObj.push(obj[i]) }
slice
let newArr = obj.slice();
Array.from
let newArr = Array.from(obj);
Open this result on MeasureThat.net