reduce-spread-vs-concat-vs-push (v3)

Revision 3 of this benchmark created on


Description

Compare the O(N²) spread with a O(N) push variants. Also compare them to the more ergonomic concat version, which, while a bit faster than spread, is much slower than push due to copying (at least in this example).

https://biomejs.dev/linter/rules/no-accumulating-spread/

Setup

var arr = []
for (let i = 0; i < 1000; ++i) {
    arr.push(crypto.randomUUID())
}

Test runner

Ready to run.

Testing in
TestOps/sec
spread
arr.reduce((acc, s) => [...acc, s], [])
ready
concat
arr.reduce((acc, s) => acc.concat([s]), [])
ready
push-and-return
arr.reduce((acc, s) => {acc.push(s); return acc}, [])
ready
push-and-comma
arr.reduce((acc, s) => (acc.push(s), acc), [])
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.