-
-
Save b2whats/c9813d523fd015e61472b9258703e6ef to your computer and use it in GitHub Desktop.
join vs concat when dealing with very long lists of single-character strings (http://jsbench.github.io/#c9813d523fd015e61472b9258703e6ef) #jsbench #jsperf
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"/> | |
<title>join vs concat when dealing with very long lists of single-character strings</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> | |
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
</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
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
Benchmark.prototype.setup = function () { | |
var i = 0; | |
var DATA = [ | |
"a0","a1","a2","a3","a4","a5","a6","a7","a8","a9","a10","a11","a12","a13","a14","a15","a16","a17","a18","a19","a20","a21","a22","a23","a24","a25","a26","a27","a28","a29","a30","a31","a32","a33","a34","a35","a36","a37","a38","a39","a40","a41","a42","a43","a44","a45","a46","a47","a48","a49","a50","a51","a52","a53","a54","a55","a56","a57","a58","a59","a60","a61","a62","a63","a64","a65","a66","a67","a68","a69","a70","a71","a72","a73","a74","a75","a76","a77","a78","a79","a80","a81","a82","a83","a84","a85","a86","a87","a88","a89","a90","a91","a92","a93","a94","a95","a96","a97","a98","a99" | |
]; | |
function switchTest(stuff) { | |
switch (stuff) { | |
case "a0": | |
return "f0"; | |
break; | |
case "a1": | |
return "f1"; | |
break; | |
case "a2": | |
return "f2"; | |
break; | |
case "a3": | |
return "f3"; | |
break; | |
case "a4": | |
return "f4"; | |
break; | |
case "a5": | |
return "f5"; | |
break; | |
case "a6": | |
return "f6"; | |
break; | |
case "a7": | |
return "f7"; | |
break; | |
case "a8": | |
return "f8"; | |
break; | |
case "a9": | |
return "f9"; | |
break; | |
case "a10": | |
return "f10"; | |
break; | |
case "a11": | |
return "f11"; | |
break; | |
case "a12": | |
return "f12"; | |
break; | |
case "a13": | |
return "f13"; | |
break; | |
case "a14": | |
return "f14"; | |
break; | |
case "a15": | |
return "f15"; | |
break; | |
case "a16": | |
return "f16"; | |
break; | |
case "a17": | |
return "f17"; | |
break; | |
case "a18": | |
return "f18"; | |
break; | |
case "a19": | |
return "f19"; | |
break; | |
case "a20": | |
return "f20"; | |
break; | |
case "a21": | |
return "f21"; | |
break; | |
case "a22": | |
return "f22"; | |
break; | |
case "a23": | |
return "f23"; | |
break; | |
case "a24": | |
return "f24"; | |
break; | |
case "a25": | |
return "f25"; | |
break; | |
case "a26": | |
return "f26"; | |
break; | |
case "a27": | |
return "f27"; | |
break; | |
case "a28": | |
return "f28"; | |
break; | |
case "a29": | |
return "f29"; | |
break; | |
case "a30": | |
return "f30"; | |
break; | |
case "a31": | |
return "f31"; | |
break; | |
case "a32": | |
return "f32"; | |
break; | |
case "a33": | |
return "f33"; | |
break; | |
case "a34": | |
return "f34"; | |
break; | |
case "a35": | |
return "f35"; | |
break; | |
case "a36": | |
return "f36"; | |
break; | |
case "a37": | |
return "f37"; | |
break; | |
case "a38": | |
return "f38"; | |
break; | |
case "a39": | |
return "f39"; | |
break; | |
case "a40": | |
return "f40"; | |
break; | |
case "a41": | |
return "f41"; | |
break; | |
case "a42": | |
return "f42"; | |
break; | |
case "a43": | |
return "f43"; | |
break; | |
case "a44": | |
return "f44"; | |
break; | |
case "a45": | |
return "f45"; | |
break; | |
case "a46": | |
return "f46"; | |
break; | |
case "a47": | |
return "f47"; | |
break; | |
case "a48": | |
return "f48"; | |
break; | |
case "a49": | |
return "f49"; | |
break; | |
case "a50": | |
return "f50"; | |
break; | |
case "a51": | |
return "f51"; | |
break; | |
case "a52": | |
return "f52"; | |
break; | |
case "a53": | |
return "f53"; | |
break; | |
case "a54": | |
return "f54"; | |
break; | |
case "a55": | |
return "f55"; | |
break; | |
case "a56": | |
return "f56"; | |
break; | |
case "a57": | |
return "f57"; | |
break; | |
case "a58": | |
return "f58"; | |
break; | |
case "a59": | |
return "f59"; | |
break; | |
case "a60": | |
return "f60"; | |
break; | |
case "a61": | |
return "f61"; | |
break; | |
case "a62": | |
return "f62"; | |
break; | |
case "a63": | |
return "f63"; | |
break; | |
case "a64": | |
return "f64"; | |
break; | |
case "a65": | |
return "f65"; | |
break; | |
case "a66": | |
return "f66"; | |
break; | |
case "a67": | |
return "f67"; | |
break; | |
case "a68": | |
return "f68"; | |
break; | |
case "a69": | |
return "f69"; | |
break; | |
case "a70": | |
return "f70"; | |
break; | |
case "a71": | |
return "f71"; | |
break; | |
case "a72": | |
return "f72"; | |
break; | |
case "a73": | |
return "f73"; | |
break; | |
case "a74": | |
return "f74"; | |
break; | |
case "a75": | |
return "f75"; | |
break; | |
case "a76": | |
return "f76"; | |
break; | |
case "a77": | |
return "f77"; | |
break; | |
case "a78": | |
return "f78"; | |
break; | |
case "a79": | |
return "f79"; | |
break; | |
case "a80": | |
return "f80"; | |
break; | |
case "a81": | |
return "f81"; | |
break; | |
case "a82": | |
return "f82"; | |
break; | |
case "a83": | |
return "f83"; | |
break; | |
case "a84": | |
return "f84"; | |
break; | |
case "a85": | |
return "f85"; | |
break; | |
case "a86": | |
return "f86"; | |
break; | |
case "a87": | |
return "f87"; | |
break; | |
case "a88": | |
return "f88"; | |
break; | |
case "a89": | |
return "f89"; | |
break; | |
case "a90": | |
return "f90"; | |
break; | |
case "a91": | |
return "f91"; | |
break; | |
case "a92": | |
return "f92"; | |
break; | |
case "a93": | |
return "f93"; | |
break; | |
case "a94": | |
return "f94"; | |
break; | |
case "a95": | |
return "f95"; | |
break; | |
case "a96": | |
return "f96"; | |
break; | |
case "a97": | |
return "f97"; | |
break; | |
case "a98": | |
return "f98"; | |
break; | |
case "a99": | |
return "f99"; | |
break; | |
} | |
}; | |
const table = { | |
"a0": "f0", | |
"a1": "f1", | |
"a2": "f2", | |
"a3": "f3", | |
"a4": "f4", | |
"a5": "f5", | |
"a6": "f6", | |
"a7": "f7", | |
"a8": "f8", | |
"a9": "f9", | |
"a10": "f10", | |
"a11": "f11", | |
"a12": "f12", | |
"a13": "f13", | |
"a14": "f14", | |
"a15": "f15", | |
"a16": "f16", | |
"a17": "f17", | |
"a18": "f18", | |
"a19": "f19", | |
"a20": "f20", | |
"a21": "f21", | |
"a22": "f22", | |
"a23": "f23", | |
"a24": "f24", | |
"a25": "f25", | |
"a26": "f26", | |
"a27": "f27", | |
"a28": "f28", | |
"a29": "f29", | |
"a30": "f30", | |
"a31": "f31", | |
"a32": "f32", | |
"a33": "f33", | |
"a34": "f34", | |
"a35": "f35", | |
"a36": "f36", | |
"a37": "f37", | |
"a38": "f38", | |
"a39": "f39", | |
"a40": "f40", | |
"a41": "f41", | |
"a42": "f42", | |
"a43": "f43", | |
"a44": "f44", | |
"a45": "f45", | |
"a46": "f46", | |
"a47": "f47", | |
"a48": "f48", | |
"a49": "f49", | |
"a50": "f50", | |
"a51": "f51", | |
"a52": "f52", | |
"a53": "f53", | |
"a54": "f54", | |
"a55": "f55", | |
"a56": "f56", | |
"a57": "f57", | |
"a58": "f58", | |
"a59": "f59", | |
"a60": "f60", | |
"a61": "f61", | |
"a62": "f62", | |
"a63": "f63", | |
"a64": "f64", | |
"a65": "f65", | |
"a66": "f66", | |
"a67": "f67", | |
"a68": "f68", | |
"a69": "f69", | |
"a70": "f70", | |
"a71": "f71", | |
"a72": "f72", | |
"a73": "f73", | |
"a74": "f74", | |
"a75": "f75", | |
"a76": "f76", | |
"a77": "f77", | |
"a78": "f78", | |
"a79": "f79", | |
"a80": "f80", | |
"a81": "f81", | |
"a82": "f82", | |
"a83": "f83", | |
"a84": "f84", | |
"a85": "f85", | |
"a86": "f86", | |
"a87": "f87", | |
"a88": "f88", | |
"a89": "f89", | |
"a90": "f90", | |
"a91": "f91", | |
"a92": "f92", | |
"a93": "f93", | |
"a94": "f94", | |
"a95": "f95", | |
"a96": "f96", | |
"a97": "f97", | |
"a98": "f98", | |
"a99": "f99", | |
} | |
var lookupTableCached = function(blud) { | |
return table[blud]; | |
}; | |
}; | |
suite.add("switchTest(DATA[Math.floor(Math.random() * DATA.length)])", function () { | |
switchTest(DATA[Math.floor(Math.random() * DATA.length)]) | |
}); | |
suite.add("lookupTableCached(DATA[Math.floor(Math.random() * DATA.length)])", function () { | |
lookupTableCached(DATA[Math.floor(Math.random() * DATA.length)]) | |
}); | |
suite.add("switchTest('a'+1)", function () { | |
switchTest('a'+1) | |
}); | |
suite.add("switchTest('a1')", function () { | |
switchTest('a1') | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("join vs concat when dealing with very long lists of single-character strings"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment