Created
June 23, 2016 05:34
-
-
Save zz85/25e33c5b2a1248a866764b4243ce4315 to your computer and use it in GitHub Desktop.
Fastest Array Joins on Strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var array = ['livechat', 'visitors', 'apple', 'display_name']; | |
| function testConcat(y) { | |
| return array.concat([y]); | |
| } | |
| function testDeconstruct(y) { | |
| return [...array, y]; | |
| } | |
| function testBabel(y) { | |
| return [].concat(_toConsumableArray(array), [y]); | |
| } | |
| function testSplice(y) { | |
| return array.concat().splice(array.length, 0, y); | |
| } | |
| function testPush(y) { | |
| var a = array.concat(); | |
| a.push(y); | |
| return a; | |
| } | |
| function testMakeNew(y) { | |
| var a = new Array(array.length); | |
| for (var i =0; i < array.length; i++) { | |
| a.push(array[i]); | |
| } | |
| a.push(y); | |
| return a; | |
| } | |
| function join2(array, y) { | |
| var a = new Array(array.length + 1); | |
| for (var i =0; i < a.length; i++) { | |
| a[i] = (array[i]); | |
| } | |
| a[a.length - 1] = y; | |
| return a; | |
| } | |
| function testMakeNew2(y) { | |
| join2(array, y); | |
| } | |
| function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | |
| var many = ['ali', 'banana', 'somewan', 'poot', 'yli']; | |
| var iterations = 100000; | |
| console.time('concat'); | |
| for (var i = 0; i < iterations; i++) many.forEach(testConcat); | |
| console.timeEnd('concat'); | |
| console.time('deconstruct'); | |
| for (var i = 0; i < iterations; i++) many.forEach(testDeconstruct); | |
| console.timeEnd('deconstruct'); | |
| console.time('deconstruct2'); | |
| for (var i = 0; i < iterations; i++) many.forEach(testBabel); | |
| console.timeEnd('deconstruct2'); | |
| console.time('deconstruct3'); | |
| for (var i = 0; i < iterations; i++) many.forEach(testSplice); | |
| console.timeEnd('deconstruct3'); | |
| console.time('deconstruct4'); | |
| for (var i = 0; i < iterations; i++) many.forEach(testPush); | |
| console.timeEnd('deconstruct4'); | |
| console.time('deconstruct5'); | |
| for (var i = 0; i < iterations; i++) many.forEach(testMakeNew); | |
| console.timeEnd('deconstruct5'); | |
| console.time('deconstruct6'); | |
| for (var i = 0; i < iterations; i++) many.forEach(testMakeNew2); | |
| console.timeEnd('deconstruct6'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| node array_joining | |
| concat: 192.467ms | |
| deconstruct: 243.186ms | |
| deconstruct2: 279.228ms | |
| deconstruct3: 217.192ms | |
| deconstruct4: 140.693ms | |
| deconstruct5: 43.969ms | |
| deconstruct6: 37.521ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment