Created
November 9, 2019 21:44
-
-
Save peterforgacs/3609683f453e5e66bc8234bb055d0d2e 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 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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment