Created
September 26, 2018 14:37
-
-
Save Jeff-Mott-OR/ddb18a96ca8fd45d8e4ca55c7a680379 to your computer and use it in GitHub Desktop.
Untitled benchmark (http://jsbench.github.io/#ddb18a96ca8fd45d8e4ca55c7a680379) #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>Untitled benchmark</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 () { | |
class Complex16 extends Float64Array { | |
constructor(...args) { | |
super(...args); | |
if (this.length !== 2) { | |
throw new Error('invalid complex number'); | |
} | |
} | |
// setters | |
set re(value) { this[0] = value; } | |
set im(value) { this[1] = value; } | |
// getters | |
get re() { return this[0]; } | |
get im() { return this[1]; } | |
// for visualization purposes only | |
toString() { | |
const { im, re } = this; | |
let real = ''; | |
if (re !== 0 || im === 0) { | |
real = re; | |
} | |
let op = ''; | |
if (re !== 0 && im > 0) { | |
op = '+'; | |
} | |
let imaginary = ''; | |
if (im !== 0) { | |
if (im === 1) { | |
imaginary = `i`; | |
} else if (im === -1) { | |
imaginary = `-i` | |
} else { | |
imaginary = `${im}i`; | |
} | |
} | |
return `${real}${op}${imaginary}`; | |
} | |
} | |
function add16(z1, z2) { | |
return new Complex16([ | |
z1.re + z2.re, | |
z1.im + z2.im | |
]); | |
} | |
class Complex { | |
constructor(re, im) { | |
if (re === undefined || im === undefined) { | |
throw new Error('invalid complex number'); | |
} | |
this._re = re; | |
this._im = im; | |
} | |
// setters | |
set re(value) { this._re = value; } | |
set im(value) { this._im = value; } | |
// getters | |
get re() { return this._re; } | |
get im() { return this._im; } | |
// for visualization purposes only | |
toString() { | |
const { _im, _re } = this; | |
let real = ''; | |
if (_re !== 0 || _im === 0) { | |
real = _re; | |
} | |
let op = ''; | |
if (_re !== 0 && _im > 0) { | |
op = '+'; | |
} | |
let imaginary = ''; | |
if (_im !== 0) { | |
if (_im === 1) { | |
imaginary = `i`; | |
} else if (_im === -1) { | |
imaginary = `-i` | |
} else { | |
imaginary = `${_im}i`; | |
} | |
} | |
return `${real}${op}${imaginary}`; | |
} | |
} | |
function add(z1, z2) { | |
return new Complex( | |
z1.re + z2.re, | |
z1.im + z2.im | |
); | |
} | |
}; | |
suite.add("const z1 = new Complex16([2, 1]);", function () { | |
const z1 = new Complex16([2, 1]); | |
const z2 = new Complex16([3, 1]); | |
add16(z1, z2).toString() | |
}); | |
suite.add("const z1 = new Complex(2, 1);", function () { | |
const z1 = new Complex(2, 1); | |
const z2 = new Complex(3, 1); | |
add(z1, z2).toString() | |
}); | |
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("Untitled benchmark"); | |
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