Last active
June 28, 2017 20:12
-
-
Save JobLeonard/8345886c28796a6d825c292900d01777 to your computer and use it in GitHub Desktop.
char vs charCode combined with object vs Map hasOwnProperty #jsbench #jsperf (http://jsbench.github.io/#8345886c28796a6d825c292900d01777) #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>char vs charCode combined with object vs Map hasOwnProperty #jsbench #jsperf</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 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] = false; | |
} | |
}; | |
Benchmark.prototype.teardown = function () { | |
for (var i = 0; i < converted.length; i++){ | |
if (!converted[i]){ | |
throw 'TEST FAILED!'; | |
} | |
converted[i] = false; | |
} | |
}; | |
suite.add("char object hasOwnProperty", function () { | |
// char object hasOwnProperty | |
for (var i = 0; i < randomString.length; i++){ | |
converted[i] = Object.prototype.hasOwnProperty.call(charDict, randomString.charAt(i)); | |
} | |
}); | |
suite.add("charCode object hasOwnProperty", function () { | |
// charCode object hasOwnProperty | |
for (var i = 0; i < randomString.length; i++){ | |
converted[i] = Object.prototype.hasOwnProperty.call(charCodeDict, randomString.charCodeAt(i)); | |
} | |
}); | |
suite.add("char Map has", function () { | |
// char Map has | |
for (var i = 0; i < randomString.length; i++){ | |
converted[i] = charMap.has(randomString.charAt(i)); | |
} | |
}); | |
suite.add("charCode Map has", function () { | |
// charCode Map has | |
for (var i = 0; i < randomString.length; i++){ | |
converted[i] = charCodeMap.has(randomString.charCodeAt(i)); | |
} | |
}); | |
suite.add("char object[key] !== undefined", function () { | |
// char object[key] !== undefined | |
for (var i = 0; i < randomString.length; i++){ | |
converted[i] =charDict[randomString.charAt(i)] !== undefined; | |
} | |
}); | |
suite.add("charCode object[key] !== undefined", function () { | |
// charCode object[key] !== undefined | |
for (var i = 0; i < randomString.length; i++){ | |
converted[i] = charCodeDict[randomString.charCodeAt(i)] !== undefined; | |
} | |
}); | |
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 hasOwnProperty #jsbench #jsperf"); | |
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