Skip to content

Instantly share code, notes, and snippets.

@ivorycirrus
Last active September 7, 2016 03:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivorycirrus/8831875b9331d938311f7682500c7002 to your computer and use it in GitHub Desktop.
Save ivorycirrus/8831875b9331d938311f7682500c7002 to your computer and use it in GitHub Desktop.
Javascript array deep copy with various ways.
var Test = {};
(function(scope){
scope.sampleArray = null;
scope.createSampleArray = function(_size, _seed){
scope.sampleArray = [];
for(var inx = 0 ; inx < _size ; inx++) scope.sampleArray.push(_seed);
};
//--------------------------------
scope.copyPushForIndexCount = function(){
var _size = scope.sampleArray.length;
var _arr = [];
var _start = Date.now();
for(var inx = 0 ; inx < _size ; inx++) _arr.push(scope.sampleArray[inx]);
var _end = Date.now();
return (_end-_start);
};
scope.copyPushForIn = function(){
var _arr = [];
var _start = Date.now();
for(inx in scope.sampleArray) _arr.push(scope.sampleArray[inx]);
var _end = Date.now();
return (_end-_start);
};
scope.copyPushArrayForeach = function(){
var _arr = [];
var _start = Date.now();
scope.sampleArray.forEach(function(element){
_arr.push(element);
})
var _end = Date.now();
return (_end-_start);
};
//--------------------------------
scope.copyPushForIndexCountFixedInit = function(){
var _size = scope.sampleArray.length;
var _arr = new Array(_size);
var _start = Date.now();
for(var inx = 0 ; inx < _size ; inx++) _arr.push(scope.sampleArray[inx]);
var _end = Date.now();
return (_end-_start);
};
scope.copyPushForInFixedInit = function(){
var _size = scope.sampleArray.length;
var _arr = new Array(_size);
var _start = Date.now();
for(inx in scope.sampleArray) _arr.push(scope.sampleArray[inx]);
var _end = Date.now();
return (_end-_start);
};
scope.copyPushArrayForeachFixedInit = function(){
var _size = scope.sampleArray.length;
var _arr = new Array(_size);
var _start = Date.now();
scope.sampleArray.forEach(function(element){
_arr.push(element);
})
var _end = Date.now();
return (_end-_start);
};
//--------------------------------
scope.copyUnshiftForIndexCount = function(){
var _size = scope.sampleArray.length;
var _arr = [];
var _start = Date.now();
for(var inx = _size-1 ; inx >= 0 ; inx--) _arr.unshift(scope.sampleArray[inx]);
var _end = Date.now();
return (_end-_start);
};
scope.copyUnshiftForIn = function(){
var _arr = [];
var _start = Date.now();
for(inx in scope.sampleArray) _arr.unshift(scope.sampleArray[inx]);
_arr.reverse();
var _end = Date.now();
return (_end-_start);
};
scope.copyUnshiftArrayForeach = function(){
var _arr = [];
var _start = Date.now();
scope.sampleArray.forEach(function(element){
_arr.unshift(element);
})
_arr.reverse();
var _end = Date.now();
return (_end-_start);
};
//--------------------------------
scope.copyUnshiftForIndexCountFixedInit = function(){
var _size = scope.sampleArray.length;
var _arr = new Array(_size);
var _start = Date.now();
for(var inx = _size-1 ; inx >= 0 ; inx--) _arr.unshift(scope.sampleArray[inx]);
var _end = Date.now();
return (_end-_start);
};
scope.copyUnshiftForInFixedInit = function(){
var _size = scope.sampleArray.length;
var _arr = new Array(_size);
var _start = Date.now();
for(inx in scope.sampleArray) _arr.unshift(scope.sampleArray[inx]);
_arr.reverse();
var _end = Date.now();
return (_end-_start);
};
scope.copyUnshiftArrayForeachFixedInit = function(){
var _size = scope.sampleArray.length;
var _arr = new Array(_size);
var _start = Date.now();
scope.sampleArray.forEach(function(element){
_arr.unshift(element);
})
_arr.reverse();
var _end = Date.now();
return (_end-_start);
};
//--------------------------------
scope.copyIndexForIndexCount = function(){
var _size = scope.sampleArray.length;
var _arr = [];
var _start = Date.now();
for(var inx = 0 ; inx < _size ; inx++) _arr[inx] = scope.sampleArray[inx];
var _end = Date.now();
return (_end-_start);
};
scope.copyIndexForIn = function(){
var _arr = [];
var _start = Date.now();
for(inx in scope.sampleArray) _arr[inx] = scope.sampleArray[inx];
var _end = Date.now();
return (_end-_start);
};
scope.copyIndexArrayForeach = function(){
var _arr = [];
var _start = Date.now();
scope.sampleArray.forEach(function(element, inx){
_arr[inx] = element;
})
var _end = Date.now();
return (_end-_start);
};
//--------------------------------
scope.copyIndexForIndexCountFixedInit = function(){
var _size = scope.sampleArray.length;
var _arr = new Array(_size);
var _start = Date.now();
for(var inx = 0 ; inx < _size ; inx++) _arr[inx] = scope.sampleArray[inx];
var _end = Date.now();
return (_end-_start);
};
scope.copyIndexForInFixedInit = function(){
var _size = scope.sampleArray.length;
var _arr = new Array(_size);
var _start = Date.now();
for(inx in scope.sampleArray) _arr[inx] = scope.sampleArray[inx];
var _end = Date.now();
return (_end-_start);
};
scope.copyIndexArrayForeachFixedInit = function(){
var _size = scope.sampleArray.length;
var _arr = new Array(_size);
var _start = Date.now();
scope.sampleArray.forEach(function(element, inx){
_arr[inx] = element;
})
var _end = Date.now();
return (_end-_start);
};
//--------------------------------
scope.copyIndexWhileIndexCount = function(){
var _arr = [];
var inx = scope.sampleArray.length;
var _start = Date.now();
while(inx--) {
_arr[inx] = scope.sampleArray[inx];
}
var _end = Date.now();
return (_end-_start);
};
scope.copyIndexWhileIndexCountFixedInit = function(){
var _size = scope.sampleArray.length;
var _arr = new Array(_size);
var inx = _size;
var _start = Date.now();
while(inx--) {
_arr[inx] = scope.sampleArray[inx];
}
var _end = Date.now();
return (_end-_start);
};
//--------------------------------
scope.copySlice = function(){
var _arr = [];
var _start = Date.now();
_arr = scope.sampleArray.slice();
var _end = Date.now();
return (_end-_start);
};
scope.copySliceZero = function(){
var _arr = [];
var _start = Date.now();
_arr = scope.sampleArray.slice(0);
var _end = Date.now();
return (_end-_start);
};
scope.copyConcat = function(){
var _arr = [];
var _start = Date.now();
_arr = scope.sampleArray.concat([]);
var _end = Date.now();
return (_end-_start);
};
scope.copyJSON = function(){
var _arr = [];
var _start = Date.now();
_arr = JSON.parse(JSON.stringify(scope.sampleArray));
var _end = Date.now();
return (_end-_start);
};
})(Test);
function startTest(){
var arraySize = 1000000,
arrayValue = 0,
samplingCount = 10;
var TestSet = {
"copyPushForIndexCount" : 0,
"copyPushForIn" : 0,
"copyPushArrayForeach" : 0,
"copyPushForIndexCountFixedInit" : 0,
"copyPushForInFixedInit" : 0,
"copyPushArrayForeachFixedInit" : 0,
"copyUnshiftForIndexCount" : 0,
"copyUnshiftForIn" : 0,
"copyUnshiftArrayForeach" : 0,
"copyUnshiftForIndexCountFixedInit" : 0,
"copyUnshiftForInFixedInit" : 0,
"copyUnshiftArrayForeachFixedInit" : 0,
"copyIndexForIndexCount" : 0,
"copyIndexForIn" : 0,
"copyIndexArrayForeach" : 0,
"copyIndexForIndexCountFixedInit" : 0,
"copyIndexForInFixedInit" : 0,
"copyIndexArrayForeachFixedInit" : 0,
"copyIndexWhileIndexCount" : 0,
"copyIndexWhileIndexCountFixedInit" : 0,
"copySlice" : 0,
"copySliceZero" : 0,
"copyConcat" : 0,
"copyJSON" : 0
};
Test.createSampleArray(arraySize, arrayValue);
console.log("========================================================");
console.log("Copying array - size : "+arraySize+" / sampling count : "+samplingCount);
console.log("--------------------------------------------------------");
for(TestName in TestSet){
for(var inx = 0 ; inx < samplingCount ; inx++) {
TestSet[TestName] += Test[TestName]();
}
TestSet[TestName] /= samplingCount;
console.log(TestName + " : " + TestSet[TestName] + "ms")
}
console.log("========================================================");
}
startTest();
@ivorycirrus
Copy link
Author

ivorycirrus commented Aug 23, 2016

Test Env.

- H/W : MacBook Pro (Retina, 13-inch, Late 2013) , i5 , 8G ram
- OS : OSX El Capitan 10.11.6
- Tool : node.js v4.2.5

Result

========================================================
Copying array - size : 100000 / sampling count : 10
--------------------------------------------------------
copyPushForIndexCount : 1.8ms
copyPushForIn : 43.2ms
copyPushArrayForeach : 3ms
copyPushForIndexCountFixedInit : 5.6ms
copyPushForInFixedInit : 45.4ms
copyPushArrayForeachFixedInit : 4.9ms
copyUnshiftForIndexCount : 1943.9ms
copyUnshiftForIn : 1983.5ms
copyUnshiftArrayForeach : 1934.8ms
copyUnshiftForIndexCountFixedInit : 17118.9ms
copyUnshiftForInFixedInit : 26391.2ms
copyUnshiftArrayForeachFixedInit : 26395.6ms
copyIndexForIndexCount : 2ms
copyIndexForIn : 54.9ms
copyIndexArrayForeach : 2.2ms
copyIndexForIndexCountFixedInit : 0.2ms
copyIndexForInFixedInit : 47.1ms
copyIndexArrayForeachFixedInit : 1.9ms
copyIndexWhileIndexCount : 7.5ms
copyIndexWhileIndexCountFixedInit : 0.4ms
copySlice : 0.2ms
copySliceZero : 0.2ms
copyConcat : 0.1ms
copyJSON : 4.7ms
========================================================

Havier test for within 1ms in previous result

========================================================
Copying array - size : 10000000 / sampling count : 10
--------------------------------------------------------
copyIndexForIndexCountFixedInit : 16.2ms
copyIndexWhileIndexCountFixedInit : 18.2ms
copySlice : 49.6ms
copySliceZero : 49.2ms
copyConcat : 49.3ms
========================================================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment