function multiArgs(a, b, c, d, e, f, g, h) {
return a + b + c + d + e + f + g + h
}
function multiArgs2(args) {
return args.reduce((acc, val) => acc += val, 0)
}
function singleArg(obj) {
return obj.a + obj.b + obj.c + obj.d + obj.e + obj.f + obj.g + obj.h
}
function singleArg2(obj) {
return Object.values(obj).reduce((acc, val) => acc += val, 0)
}
for (let i = 0; i < 10000; ++i) {
multiArgs(1, 2, 3, 4, 5, 6, 7, 8)
}
for (let i = 0; i < 10000; ++i) {
multiArgs2(1, 2, 3, 4, 5, 6, 7, 8)
}
for (let i = 0; i < 10000; ++i) {
singleArg({ a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8 })
}
for (let i = 0; i < 10000; ++i) {
singleArg2({ a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8 })
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Multi args (1) | |
Multi args (2) | |
Single arg (1) | |
Single arg (2) |
Test name | Executions per second |
---|---|
Multi args (1) | 98982.9 Ops/sec |
Multi args (2) | 2594.4 Ops/sec |
Single arg (1) | 132811.3 Ops/sec |
Single arg (2) | 621.8 Ops/sec |
I'd be happy to help explain the benchmark.
Overview
The benchmark measures the performance of three different approaches for calculating the sum of eight numbers:
multiArgs(a, b, c, d, e, f, g, h)
multiArgs2(...args)
singleArg(obj)
Options Compared
The benchmark compares the performance of each approach on a dataset of 10000 iterations with different input values.
Pros and Cons
Other Considerations
Library and Special JS Features
There are no libraries involved in this benchmark.
No special JavaScript features like async/await or ES6 classes are used.
Alternatives
If you're looking for alternative approaches, consider the following:
reduce()
(for arrays) and Object.values()
. These might be more efficient than using custom functions.The provided benchmark allows users to compare the performance of different approaches on a microbenchmarking scale, providing insights into optimization techniques for specific use cases.