Last active
October 7, 2020 06:37
-
-
Save milahu/9ea2f56bba30fed35f1b5f2b22954d4e to your computer and use it in GitHub Desktop.
test if type is string (http://jsbench.github.io/#9ea2f56bba30fed35f1b5f2b22954d4e) #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>test if type is string</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 () { | |
const str = "hello world"; | |
const str_object = new String("hello world"); | |
const null_var = null; | |
}; | |
suite.add("string: type or instance", function () { | |
// string: type or instance | |
// fastest | |
typeof str == 'string' || str instanceof String | |
}); | |
suite.add("object: type or instance", function () { | |
// object: type or instance | |
// fastest | |
typeof str_object == 'string' || str_object instanceof String | |
}); | |
suite.add("null: type or instance", function () { | |
// null: type or instance | |
// fastest | |
typeof null_var == 'string' || null_var instanceof String | |
}); | |
suite.add("string: toString", function () { | |
// string: toString | |
// this one is really slow, 30x slower than the fastest versions | |
// slow cos str is converted to str_object | |
Object.prototype.toString.call(str) == '[object String]' | |
}); | |
suite.add("object: toString", function () { | |
// object: toString | |
Object.prototype.toString.call(str_object) == '[object String]' | |
}); | |
suite.add("null: toString", function () { | |
// null: toString | |
// this is slow too | |
Object.prototype.toString.call(null_var) == '[object String]' | |
}); | |
suite.add("string: charCodeAt", function () { | |
// string: charCodeAt | |
// this is slow too | |
Boolean(str && str.charCodeAt) | |
}); | |
suite.add("object: charCodeAt", function () { | |
// object: charCodeAt | |
Boolean(str_object && str_object.charCodeAt) | |
}); | |
suite.add("null: charCodeAt", function () { | |
// null: charCodeAt | |
Boolean(null_var && null_var.charCodeAt) | |
}); | |
suite.add("string: charCodeAt try-catch", function () { | |
// string: charCodeAt try-catch | |
try { Boolean(str.charCodeAt); } | |
catch (_) { false; } | |
}); | |
suite.add("object: charCodeAt try-catch", function () { | |
// object: charCodeAt try-catch | |
try { Boolean(str_object.charCodeAt); } | |
catch (_) { false; } | |
}); | |
suite.add("null: charCodeAt try-catch", function () { | |
// null: charCodeAt try-catch | |
// this is slow cos of the catch(_) overhead | |
try { Boolean(null_var.charCodeAt); } | |
catch (_) { false; } | |
}); | |
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("test if type is string"); | |
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