Skip to content

Instantly share code, notes, and snippets.

@bmeurer
Created August 1, 2017 12:42
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 bmeurer/cc4a6c97d244eb4c8c0738bd4b8c3319 to your computer and use it in GitHub Desktop.
Save bmeurer/cc4a6c97d244eb4c8c0738bd4b8c3319 to your computer and use it in GitHub Desktop.
class A {
get [Symbol.toStringTag]() { return "A"; }
}
const numbers = [0, 0.5, 1, Math.pow(2,32)-1, Infinity];
const strings = ["", '0', 'abcde', "Hello World", '6'.repeat(100)];
const objects = [{}, [], new A, Object.prototype, x => x];
const values = [{}, 0, '', x => x, []];
const n = 1e6;
function Numbers() {
let result = "";
for (const number of numbers) {
for (let i = 0; i < n; ++i) {
result = Object.prototype.toString.call(number);
}
}
return result;
}
function Strings() {
let result = "";
for (const string of strings) {
for (let i = 0; i < n; ++i) {
result = Object.prototype.toString.call(string);
}
}
return result;
}
function Objects() {
let result = "";
for (const object of objects) {
for (let i = 0; i < n; ++i) {
result = Object.prototype.toString.call(object);
}
}
return result;
}
function Values() {
let result = "";
for (const value of values) {
for (let i = 0; i < n; ++i) {
result = Object.prototype.toString.call(value);
}
}
return result;
}
var TESTS = [
Numbers,
Strings,
Objects,
Values
];
// Warmup.
for (let i = 0; i < 10; ++i) {
for (const test of TESTS) test();
}
// Actual tests.
for (const test of TESTS) {
console.time(test.name);
test();
console.timeEnd(test.name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment