Skip to content

Instantly share code, notes, and snippets.

@zspitzer
Forked from nolanlawson/index.html
Last active December 30, 2016 23:16
Show Gist options
  • Save zspitzer/8276e7919207b2417df11d956272801c to your computer and use it in GitHub Desktop.
Save zspitzer/8276e7919207b2417df11d956272801c to your computer and use it in GitHub Desktop.
bracket notation vs charAt (http://jsbench.github.io/#8276e7919207b2417df11d956272801c) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>bracket notation vs charAt</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>
"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 strings = [
'lift:ec7e236a-c199-11e6-97f2-54ee758131fa' ,
'lift:ec7ed95e-c199-11e6-97f2-54ee758131fa' ,
'ec7f0122-c199-11e6-97f2-54ee758131fa' ,
'lift:ec7f2968-c199-11e6-97f2-54ee758131fa' ,
'ec7f4740-c199-11e6-97f2-54ee758131fa' ,
'ec7f5e56-c199-11e6-97f2-54ee758131fa' ,
'lift:ec7f7576-c199-11e6-97f2-54ee758131fa' ,
'ec7f8ade-c199-11e6-97f2-54ee758131fa' ,
'ec7fa488-c199-11e6-97f2-54ee758131fa' ,
'lift:ec7fbf04-c199-11e6-97f2-54ee758131fa' ,
'ec7fd94e-c199-11e6-97f2-54ee758131fa' ,
'ec7ff348-c199-11e6-97f2-54ee758131fa' ,
'lift:ec801666-c199-11e6-97f2-54ee758131fa' ,
'lift:ec81443c-c199-11e6-97f2-54ee758131fa' ,
'lift:ec815684-c199-11e6-97f2-54ee758131fa' ,
'lift:ec81772c-c199-11e6-97f2-54ee758131fa' ,
'lift:ec818d5c-c199-11e6-97f2-54ee758131fa' ,
'lift:ec81a206-c199-11e6-97f2-54ee758131fa' ,
'lift:ec81b6a6-c199-11e6-97f2-54ee758131fa' ,
'lift:ec81cbd2-c199-11e6-97f2-54ee758131fa' ,
'lift:ec81df46-c199-11e6-97f2-54ee758131fa' ,
'lift:ec81f512-c199-11e6-97f2-54ee758131fa' ,
'ec820a2a-c199-11e6-97f2-54ee758131fa' ,
'lift:ec822028-c199-11e6-97f2-54ee758131fa' ,
'ec8281c6-c199-11e6-97f2-54ee758131fa' ,
'ec845b5e-c199-11e6-97f2-54ee758131fa' ,
'lift:ec84853e-c199-11e6-97f2-54ee758131fa' ,
'ec84ab4a-c199-11e6-97f2-54ee758131fa' ,
'ec84c90e-c199-11e6-97f2-54ee758131fa' ,
'lift:ec84e86c-c199-11e6-97f2-54ee758131fa' ,
'ec84fff0-c199-11e6-97f2-54ee758131fa' ,
'ec8511b6-c199-11e6-97f2-54ee758131fa' ,
'lift:ec852b92-c199-11e6-97f2-54ee758131fa' ,
'lift:ec8550ae-c199-11e6-97f2-54ee758131fa' ,
'lift:ec857c0a-c199-11e6-97f2-54ee758131fa' ,
'ec8654e0-c199-11e6-97f2-54ee758131fa' ,
'ec866f5c-c199-11e6-97f2-54ee758131fa' ,
'ec8687d0-c199-11e6-97f2-54ee758131fa' ,
'ec86a026-c199-11e6-97f2-54ee758131fa' ,
'ec86b7be-c199-11e6-97f2-54ee758131fa' ,
'ec86cdb2-c199-11e6-97f2-54ee758131fa' ,
'ec86e4be-c199-11e6-97f2-54ee758131fa' ,
'ec86fa94-c199-11e6-97f2-54ee758131fa' ,
'ec8712d6-c199-11e6-97f2-54ee758131fa' ,
'ec872a00-c199-11e6-97f2-54ee758131fa' ,
'ec874396-c199-11e6-97f2-54ee758131fa' ,
'ec877302-c199-11e6-97f2-54ee758131fa' ,
'lift:ec897d6e-c199-11e6-97f2-54ee758131fa' ,
'lift:ec89aba4-c199-11e6-97f2-54ee758131fa' ,
'lift:ec89db24-c199-11e6-97f2-54ee758131fa'
]
};
suite.add("strings.forEach(str => str[0] === 'l')", function () {
strings.forEach(str => str[0] === 'l')
});
suite.add("strings.forEach(str => str.charAt(0) === 'l')", function () {
strings.forEach(str => str.charAt(0) === 'l')
});
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("bracket notation vs charAt");
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