Last active
June 28, 2017 20:10
-
-
Save JobLeonard/f1f9afdba2eac615645c80335c48ed10 to your computer and use it in GitHub Desktop.
char vs charCode combined with object vs Map lookup (http://jsbench.github.io/#f1f9afdba2eac615645c80335c48ed10) #jsbench #jsperf
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>char vs charCode combined with object vs Map lookup</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 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
"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 base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; | |
var charDict = (function (){ | |
var dict = {}; | |
dict[-1] = 0; | |
for (var i = 0; i < base64.length; i++){ | |
dict[base64.charAt(i)] = i; | |
} | |
return dict; | |
})(); | |
var charCodeDict = (function () { | |
var dict = {}; | |
dict[-1] = 0; | |
for (var i = 0; i < base64.length; i++){ | |
dict[base64.charCodeAt(i)] = i; | |
} | |
return dict; | |
})(); | |
var charMap = (function (){ | |
var dict = new Map(); | |
dict.set(-1, 0); | |
for (var i = 0; i < base64.length; i++){ | |
dict.set(base64.charAt(i), i); | |
} | |
return dict; | |
})(); | |
var charCodeMap = (function (){ | |
var dict = new Map(); | |
dict.set(-1, 0); | |
for (var i = 0; i < base64.length; i++){ | |
dict.set(base64.charCodeAt(i), i); | |
} | |
return dict; | |
})(); | |
var randomString = (function (){ | |
var i = 100000, strArray = new Array(i); | |
while(i--){ | |
strArray[i] = base64.charAt(Math.random(base64.length)|0); | |
} | |
return strArray.join(''); | |
})(); | |
var converted = new Array(randomString.length); | |
for (var i = 0; i < converted.length; i++){ | |
converted[i] = -1; | |
} | |
var test = []; | |
for (var i = 0; i < randomString.length; i++){ | |
test.push(charDict[randomString.charAt(i)]); | |
} | |
}; | |
Benchmark.prototype.teardown = function () { | |
var i = test.length; | |
while(i--){ | |
if (test[i] !== converted[i]){ | |
throw "Conversion went wrong!"; | |
} | |
converted[i] = -1; | |
} | |
}; | |
suite.add("char object lookup", function () { | |
// char object lookup | |
for (var i = 0; i < randomString.length; i++){ | |
converted[i] = charDict[randomString.charAt(i)]; | |
} | |
}); | |
suite.add("charCode object lookup", function () { | |
// charCode object lookup | |
for (var i = 0; i < randomString.length; i++){ | |
converted[i] = charCodeDict[randomString.charCodeAt(i)]; | |
} | |
}); | |
suite.add("char Map lookup", function () { | |
// char Map lookup | |
for (var i = 0; i < randomString.length; i++){ | |
converted[i] = charMap.get(randomString.charAt(i)); | |
} | |
}); | |
suite.add("charCode Map lookup", function () { | |
// charCode Map lookup | |
for (var i = 0; i < randomString.length; i++){ | |
converted[i] = charCodeMap.get(randomString.charCodeAt(i)); | |
} | |
}); | |
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("char vs charCode combined with object vs Map lookup"); | |
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