var a = "Here's a string value";
var b = 5; // and a number
var c = false;
var object = {
a, b, c
}
var array = [
a, b, c
];
var passObject = (obj) => {
return obj.a.length + obj.b * obj.c ? 2 : 1;
}
var passRawValues = (val_a, val_b, val_c) => {
return val_a.length + val_b * val_c ? 2 : 1;
}
var passArray = (arr) => {
return arr[0].length + arr[1] * arr[2] ? 2 : 1;
}
var x = 0;
x << 1;
x ^= passObject(object);
x << 1;
x ^= passRawValues(a, b, c);
x << 1;
x ^= passArray(array);
x << 1;
x ^= passObject({a, b, c});
object.a = a;
object.b = b;
object.c = c;
x << 1;
x ^= passObject(object);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Pass object | |
Pass raw values | |
Pass array | |
Pass new object | |
Pass re-created object |
Test name | Executions per second |
---|---|
Pass object | 3894234.2 Ops/sec |
Pass raw values | 2746705.0 Ops/sec |
Pass array | 3876710.0 Ops/sec |
Pass new object | 2765405.8 Ops/sec |
Pass re-created object | 1771387.2 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Definition
The benchmark compares the performance of passing objects with raw values, passing new objects with raw values, passing just raw values, and passing an array of values. This is done to determine which approach is the most efficient in terms of execution speed.
Test Cases
There are five test cases:
object
) to the passObject
function.a
, b
, and c
) to the passRawValues
function.array
) to the passArray
function.{a, b, c}
) and passes it to the passObject
function.object
) and then passing it to the passObject
function.Library
No external library is used in this benchmark.
JavaScript Features/Syntax
The following JavaScript features or syntax are used:
{a, b, c}
creates a new object with properties a
, b
, and c
.(obj) => { ... }
defines an arrow function that takes an argument obj
.Pros/Cons of Each Approach
Here are some general pros and cons for each approach:
Other Considerations
new
or an object literal syntax may incur additional overhead compared to re-creating an existing object.Alternatives
Other alternatives for passing data between functions include:
Keep in mind that these alternatives may have their own trade-offs and performance implications.