Created
November 9, 2019 21:41
-
-
Save peterforgacs/f5015e58ed0e0cfdfe241b257491249e to your computer and use it in GitHub Desktop.
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
const { Benchmark } = require("benchmark"); | |
function generateRandomNumberBetween(min, max){ | |
return Math.floor(Math.random() * max) + min; | |
} | |
function generateTupleArray(length) { | |
const tupleArray = []; | |
for (let i = 0; i < length; i++) { | |
tupleArray.push([generateRandomNumberBetween(1, 1e3), generateRandomNumberBetween(1, 1e3)]); | |
} | |
return tupleArray; | |
} | |
// Contains 100 arrays containing elements between 1 and 1000 | |
const arrays = []; | |
for (let i = 0; i < 100; i++) { | |
arrays.push(generateTupleArray(generateRandomNumberBetween(1, 1e3))) | |
} | |
const suite = new Benchmark.Suite(); | |
suite.add("acc.concat(curr)", function() { | |
for (const arr of arrays) { | |
arr.reduce((acc, curr) => acc.concat(curr), []); | |
} | |
}); | |
suite.add("[...acc, ...curr]", function () { | |
for (const arr of arrays) { | |
arr.reduce((acc, curr) => [...acc, ...curr], []); | |
} | |
}); | |
suite.add("[].concat(...data)", function() { | |
for (const arr of arrays) { | |
[].concat(...arr); | |
} | |
}); | |
suite.on("cycle", function(event) { | |
console.log(String(event.target)); | |
}); | |
suite.on("complete", function() { | |
console.log("Fastest is " + this.filter("fastest").map("name")); | |
}) | |
suite.run(); |
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
let arr = [ [1, 2], [3, 4]]; | |
// version 1 | |
arr.reduce((acc, curr) => acc.concat(curr), []); | |
// version 2 | |
arr.reduce((acc, curr) => [...acc, ...curr], []); | |
// version 3 | |
[].concat(...arr); |
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
const { Benchmark } = require("benchmark"); | |
function generateRandomString(length) { | |
var result = ""; | |
var characters = | |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
var charactersLength = characters.length; | |
for (var i = 0; i < length; i++) { | |
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | |
} | |
return result; | |
} | |
function generateRandomNumberBetween(min, max){ | |
return Math.floor(Math.random() * max) + min; | |
} | |
// data contains 100 random strings with lneght between 1 and 1000 | |
const data = []; | |
for (let i = 0; i < 100; i++) { | |
data.push(generateRandomString(generateRandomNumberBetween(1, 1000))); | |
} | |
const suite = new Benchmark.Suite(); | |
suite.add("string.split()", function() { | |
for (const str of data) { | |
str.split(""); | |
} | |
}); | |
suite.add("Object spread", function () { | |
for (const str of data) { | |
[...str]; | |
} | |
}); | |
suite.add("Array.from()", function() { | |
for (const str of data) { | |
Array.from(str); | |
} | |
}); | |
suite.on("cycle", function(event) { | |
console.log(String(event.target)); | |
}); | |
suite.on("complete", function() { | |
console.log("Fastest is " + this.filter("fastest").map("name")); | |
}) | |
suite.run(); |
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
// version 1 | |
"hello".split("").reverse(); | |
// version 2 | |
[..."hello"].reverse(); | |
// version 3 | |
Array.from("hello").reverse(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment