Skip to content

Instantly share code, notes, and snippets.

@jasonsturges
Last active March 4, 2021 17:23
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 jasonsturges/a81c3d52ff50b67d5eed38b8215e29f5 to your computer and use it in GitHub Desktop.
Save jasonsturges/a81c3d52ff50b67d5eed38b8215e29f5 to your computer and use it in GitHub Desktop.
Drop Decimal Test Suite
double not operator
    min: 6.25000    avg: 8.41969    max: 11.04000    ops/second: 123,911,231

or operator
    min: 7.30000    avg: 9.52969    max: 10.75000    ops/second: 107,329,250

signed right shift operator
    min: 7.07500    avg: 10.00412   max: 10.79500    ops/second: 101,645,033

parseInt
    min: 11.74500   avg: 15.21844   max: 16.36500    ops/second: 66,107,377

round
    min: 7.74000    avg: 11.05765   max: 11.87000    ops/second: 91,572,037

floor
    min: 7.85000    avg: 10.78206   max: 12.00000    ops/second: 93,988,418

ceil
    min: 8.81500    avg: 11.52324   max: 12.36500    ops/second: 87,225,180

trunc
    min: 8.92000    avg: 11.53265   max: 12.11500    ops/second: 87,089,952

toFixed
    min: 247.60500  avg: 258.02000  max: 264.34000   ops/second: 3,878,004

Fastest: double not operator
Slowest: toFixed
import * as JSBench from "jsbenchmark";
new JSBench.TestSuite({
async: true,
passes: 25,
operations: 1000000,
maxRuntime: 1000,
})
.add("double not operator", () => {
~~Math.PI;
})
.add("or operator", () => {
Math.PI | 0;
})
.add("signed right shift operator", () => {
Math.PI >> 0;
})
.add("parseInt", () => {
parseInt(Math.PI);
})
.add("round", () => {
Math.round(Math.PI);
})
.add("floor", () => {
Math.floor(Math.PI);
})
.add("ceil", () => {
Math.ceil(Math.PI);
})
.add("trunc", () => {
Math.trunc(Math.PI);
})
.add("toFixed", () => {
Math.PI.toFixed();
})
.on("test", (event) => {
const test = event.test;
const resultSet = event.resultSet;
console.log(test.name);
resultSet.log();
})
.on("suite", (event) => {
const result = event.result;
result.log();
})
.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment