Created
May 8, 2025 12:30
-
-
Save jsumners/825035e5993ecfa737614fafec746868 to your computer and use it in GitHub Desktop.
Does the rest parameter syntax beat old ways?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const iterations = 1_000_000 | |
const oneStart = process.hrtime.bigint() | |
for (let i = 0; i < iterations; i += 1) { | |
curry1(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26) | |
} | |
const oneEnd = process.hrtime.bigint() | |
const twoStart = process.hrtime.bigint() | |
for (let i = 0; i < iterations; i += 1) { | |
curry2(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26) | |
} | |
const twoEnd = process.hrtime.bigint() | |
const oneDuration = oneEnd - oneStart | |
console.log('curry1 duration:', oneDuration) | |
const twoDuration = twoEnd - twoStart | |
console.log('curry2 duration:', twoDuration) | |
console.log('curry1 > curry2', oneDuration > twoDuration) | |
function curry1() { | |
// https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments | |
const len = arguments.length | |
const arr = new Array(len) | |
for (let i = 0; i < len; ++i) { | |
arr[i] = arguments[i] | |
} | |
return doSomething.apply(null, arr) | |
} | |
function curry2(...args) { | |
return doSomething.apply(null, args) | |
} | |
function doSomething(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) { | |
return a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r + s + t + u + v + w + x + y + z | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment