function isJsArray(obj_0) {
var tmp0_unsafeCast_0 = Array.isArray(obj_0);
return tmp0_unsafeCast_0;
}
function isArray(obj_0) {
var tmp;
if (isJsArray(obj_0)) {
tmp = !obj_0.$type$;
} else {
tmp = false;
}
return tmp;
}
function arrayConcat(args) {
var len = args.length;
var tmp0_unsafeCast_0 = Array(len);
var typed = tmp0_unsafeCast_0;
var inductionVariable = 0;
var last = len - 1 | 0;
if (inductionVariable <= last)
do {
var i = inductionVariable;
inductionVariable = inductionVariable + 1 | 0;
var arr = args[i];
if (!(!(arr == null) ? isArray(arr) : false)) {
typed[i] = [].slice.call(arr);
} else {
{
typed[i] = arr;
}
}
}
while (!(i === last));
return [].concat.apply([], typed);
}
var args = (new Int32Array(10000)).map(_ => Math.random(1000000));
arrayConcat([0, 1], args)
[0, 1].concat([].slice.call(args))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
legacy | |
ir |
Test name | Executions per second |
---|---|
legacy | 2435581.5 Ops/sec |
ir | 1698.8 Ops/sec |
I'll explain the provided benchmark definition, options compared, pros and cons of each approach, and other considerations.
Benchmark Definition
The provided JSON represents a JavaScript microbenchmark that compares two approaches to concatenate arrays: the legacy approach and the "IR" (Intermediate Representation) approach. The test cases are:
arrayConcat([0, 1], args)
- This is the legacy approach.[0, 1].concat([].slice.call(args))
- This is the IR approach.Options Compared
The two approaches compared in this benchmark are:
Array.isArray()
method to check if an object is an array.arrayConcat()
function, which concatenates arrays using a loop that copies elements from each input array into a new array.Array.prototype.slice.call()
to convert the input arguments to arrays.Array.isArray()
.concat()
method on an empty array created with []
.Pros and Cons of Each Approach
Pros:
Array.isArray()
returning NaN
for certain types.Cons:
arrayConcat()
.Array.prototype.slice.call()
.Pros:
concat()
.Cons:
Library Used
The Array.isArray()
method is a built-in JavaScript function that checks if an object is an array. Its purpose is to provide a convenient way to check for array-like objects, which can be useful in various scenarios.
Special JS Feature/Syntax
None of the provided code uses special JavaScript features or syntax beyond what's considered standard (ECMAScript 2020).
Other Considerations
As an alternative to these two approaches, other ways to concatenate arrays in JavaScript include:
concat()
method directly on arrays: [0, 1].concat([]).concat(args)
....
): [0, 1, ...args]
.Array.prototype.concat()
with an empty array created using []
: [0, 1].concat(new Array(0).concat(args))
.Each of these alternatives has its own trade-offs in terms of performance, readability, and maintainability.