<!--your preparation HTML code goes here-->
/*your preparation JavaScript code goes here
To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/
async function globalMeasureThatScriptPrepareFunction() {
// This function is optional, feel free to remove it.
// await someThing();
}
var getConcatenation = function(nums) {
let ans = [];
for (i = 0; i < nums.length; i++) {
ans[i] = nums[i];
ans[i + nums.length] = nums[i];
}
return ans;
};
let largeArray = Array.from({ length: 10_000_000 }, (_, i) => i);
getConcatenation(largeArray);
var getConcatenation = function (nums) {
return nums.concat(nums);
};
let largeArray = Array.from({ length: 10_000_000 }, (_, i) => i);
getConcatenation(largeArray);
var getConcatenation = function(nums) {
const len = nums.length;
for (i = 0; i < len; i++) {
nums[i + len] = nums[i];
}
return nums;
};
let largeArray = Array.from({ length: 10_000_000 }, (_, i) => i);
getConcatenation(largeArray);
/**
* @param {number[]} nums
* @return {number[]}
*/
var getConcatenation = function (nums) {
return [nums, nums];
};
let largeArray = Array.from({ length: 10_000_000 }, (_, i) => i);
getConcatenation(largeArray);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
copy | |
concat | |
in place | |
Spread operator |
Test name | Executions per second |
---|---|
copy | 1.1 Ops/sec |
concat | 3.3 Ops/sec |
in place | 1.8 Ops/sec |
Spread operator | 2.3 Ops/sec |
The benchmark focuses on comparing three different approaches for concatenating a large array (specifically, an array containing 10 million integers) in JavaScript. Each test method represents a distinct approach to achieve the same goal: creating a new array that consists of two copies of the original array.
Copy Approach:
var getConcatenation = function(nums) {
let ans = [];
for (i = 0; i < nums.length; i++) {
ans[i] = nums[i];
ans[i + nums.length] = nums[i];
}
return ans;
};
ans
. It iterates over each element of the input array nums
, copying the elements first to ans
and then appending the same elements again from the original array.Concat Approach:
var getConcatenation = function (nums) {
return nums.concat(nums);
};
concat
method of the array. It simply calls nums.concat(nums)
which technically duplicates the entire array.concat
method is concise and optimized within JavaScript’s engine, potentially executing faster than manual copying operations.In-Place Approach:
var getConcatenation = function(nums) {
const len = nums.length;
for (i = 0; i < len; i++) {
nums[i + len] = nums[i];
}
return nums;
};
The benchmark results illustrate the performance of these three methods in terms of "Executions Per Second":
concat
method may be preferable despite its slower execution time.concat
method is more readable, which may be a significant factor in collaborative environments.[...nums, ...nums]
) can also be used to concatenate arrays in a more modern and readable manner, though performance characteristics vary.push()
function in a loop to append elements manually.TypedArray
could provide performance benefits in terms of speed and memory usage, especially in numerical computations.In summary, the benchmark evaluates array concatenation techniques against a backdrop of performance, memory management, readability, and situational use cases, offering valuable insights for developers faced with similar challenges in their projects.