Created
March 8, 2017 08:03
-
-
Save hassansin/ccd5e6106815555797f8be8f29ddd949 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/lezufa
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
<script src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/2.1.3/benchmark.min.js"></script> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
console.clear(); | |
let input= [] | |
const flatten = function (arr, result) { | |
result || (result = []) | |
var i = 0; | |
for( ;i<arr.length; i++) { | |
if(Array.isArray(arr[i])) { | |
flatten(arr[i], result); | |
} else { | |
result[result.length] = arr[i] | |
} | |
} | |
return result; | |
}; | |
const flatten2 = function (arr) { | |
var result = [] | |
for(let i=0; i<arr.length; i++) { | |
if(Array.isArray(arr[i])) { | |
result = result.concat(flatten(arr[i])); | |
} else { | |
result.push(arr[i]) | |
} | |
} | |
return result; | |
} | |
const flatten3 = arr => { | |
let output = [].concat.apply([], arr) | |
for(let i=0; i<output.length; i++) { | |
if(Array.isArray(output[i])) { | |
output.splice | |
.apply(output, [i, 1].concat(flatten(output[i]))) | |
} | |
} | |
return output; | |
}; | |
console.log(flatten(input)); | |
console.log(flatten2(input)); | |
console.log(flatten3(input)); | |
var suite = new Benchmark.Suite; | |
// add tests | |
suite.add('flatten1#test', function() { | |
flatten(input) | |
}) | |
.add('flatten2#test', function() { | |
flatten2(input) | |
}) | |
.add('flatten3#test', function() { | |
flatten3(input) | |
}) | |
// add listeners | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').map('name')); | |
}) | |
// run async | |
.run({ 'async': true }); | |
</script> | |
<script id="jsbin-source-html" type="text/html"> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
<script src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"><\/script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/2.1.3/benchmark.min.js"><\/script> | |
</head> | |
<body> | |
</body> | |
</html></script> | |
<script id="jsbin-source-javascript" type="text/javascript">console.clear(); | |
let input= [] | |
const flatten = function (arr, result) { | |
result || (result = []) | |
var i = 0; | |
for( ;i<arr.length; i++) { | |
if(Array.isArray(arr[i])) { | |
flatten(arr[i], result); | |
} else { | |
result[result.length] = arr[i] | |
} | |
} | |
return result; | |
}; | |
const flatten2 = function (arr) { | |
var result = [] | |
for(let i=0; i<arr.length; i++) { | |
if(Array.isArray(arr[i])) { | |
result = result.concat(flatten(arr[i])); | |
} else { | |
result.push(arr[i]) | |
} | |
} | |
return result; | |
} | |
const flatten3 = arr => { | |
let output = [].concat.apply([], arr) | |
for(let i=0; i<output.length; i++) { | |
if(Array.isArray(output[i])) { | |
output.splice | |
.apply(output, [i, 1].concat(flatten(output[i]))) | |
} | |
} | |
return output; | |
}; | |
console.log(flatten(input)); | |
console.log(flatten2(input)); | |
console.log(flatten3(input)); | |
var suite = new Benchmark.Suite; | |
// add tests | |
suite.add('flatten1#test', function() { | |
flatten(input) | |
}) | |
.add('flatten2#test', function() { | |
flatten2(input) | |
}) | |
.add('flatten3#test', function() { | |
flatten3(input) | |
}) | |
// add listeners | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').map('name')); | |
}) | |
// run async | |
.run({ 'async': true });</script></body> | |
</html> |
This file contains 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
console.clear(); | |
let input= [] | |
const flatten = function (arr, result) { | |
result || (result = []) | |
var i = 0; | |
for( ;i<arr.length; i++) { | |
if(Array.isArray(arr[i])) { | |
flatten(arr[i], result); | |
} else { | |
result[result.length] = arr[i] | |
} | |
} | |
return result; | |
}; | |
const flatten2 = function (arr) { | |
var result = [] | |
for(let i=0; i<arr.length; i++) { | |
if(Array.isArray(arr[i])) { | |
result = result.concat(flatten(arr[i])); | |
} else { | |
result.push(arr[i]) | |
} | |
} | |
return result; | |
} | |
const flatten3 = arr => { | |
let output = [].concat.apply([], arr) | |
for(let i=0; i<output.length; i++) { | |
if(Array.isArray(output[i])) { | |
output.splice | |
.apply(output, [i, 1].concat(flatten(output[i]))) | |
} | |
} | |
return output; | |
}; | |
console.log(flatten(input)); | |
console.log(flatten2(input)); | |
console.log(flatten3(input)); | |
var suite = new Benchmark.Suite; | |
// add tests | |
suite.add('flatten1#test', function() { | |
flatten(input) | |
}) | |
.add('flatten2#test', function() { | |
flatten2(input) | |
}) | |
.add('flatten3#test', function() { | |
flatten3(input) | |
}) | |
// add listeners | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').map('name')); | |
}) | |
// run async | |
.run({ 'async': true }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment