Skip to content

Instantly share code, notes, and snippets.

@Lodin
Last active December 17, 2018 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lodin/3f43530d19a68eaba7e6b7d34eb297ab to your computer and use it in GitHub Desktop.
Save Lodin/3f43530d19a68eaba7e6b7d34eb297ab to your computer and use it in GitHub Desktop.
Case string to number performance (http://jsbench.github.io/#3f43530d19a68eaba7e6b7d34eb297ab) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Case string to number performance</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 () {
const str = '100000'
};
suite.add("const num1 = Number(str);", function () {
const num1 = Number(str);
});
suite.add("const num2 = parseInt(str);", function () {
const num2 = parseInt(str);
});
suite.add("const num3 = parseInt(str, 10);", function () {
const num3 = parseInt(str, 10);
});
suite.add("const num4 = parseFloat(str);", function () {
const num4 = parseFloat(str);
});
suite.add("const num5 = str << 0;", function () {
const num5 = str << 0;
});
suite.add("const num6 = +str;", function () {
const num6 = +str;
});
suite.add("const num7 = str * 1;", function () {
const num7 = str * 1;
});
suite.add("const num8 = str - 1;", function () {
const num8 = str - 1;
});
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("Case string to number performance");
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