Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
add seperator between array elements
(version: 0)
Comparing performance of:
flatmap vs splice loop vs reduce vs copy loop
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
flatmap
let inp = new Array(10000).fill(0) let sep = 1 inp = inp.flatMap((e, index) => index ? [sep, e] : e); console.log(inp.length, inp[0], inp[1], "...", inp.at(-2), inp.at(-1))
splice loop
let inp = new Array(10000).fill(0) let sep = 1 i = inp.length; while (--i) inp.splice(i, 0, sep); console.log(inp.length, inp[0], inp[1], "...", inp.at(-2), inp.at(-1))
reduce
let inp = new Array(10000).fill(0) let sep = 1 inp = inp.reduce((r, a) => r.concat(a, sep), []); inp.pop(); console.log(inp.length, inp[0], inp[1], "...", inp.at(-2), inp.at(-1))
copy loop
let inp = new Array(10000).fill(0) let sep = 1 let tar = new Array(inp.length * 2 - 1) for (let i = inp.length - 1; i > -1; --i) { tar[i * 2] = inp[i] tar[i * 2 - 1] = sep } inp = tar; console.log(inp.length, inp[0], inp[1], "...", inp.at(-2), inp.at(-1))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
flatmap
splice loop
reduce
copy loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Related benchmarks:
Push array 0 index with splice and spread
remove last array element (slice vs splice vs length vs pop)
delete a element and return new array with slice vs splice
Slice vs splice___
Slice vs Index
Comments
Confirm delete:
Do you really want to delete benchmark?