Created
January 24, 2023 17:12
-
-
Save amrali-eg/79ddce0e6d8bbd5c3efdd38d76ae2e35 to your computer and use it in GitHub Desktop.
Decimal Rounding - Number.EPSILON vs Number.EPSILON v2 #jsbench (https://jsbench.github.io/#79ddce0e6d8bbd5c3efdd38d76ae2e35) #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>Decimal Rounding - Number.EPSILON vs Number.EPSILON v2 #jsbench</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 () { | |
// Solution 2 | |
var DecimalPrecision2 = (function() { | |
if (Number.EPSILON === undefined) { | |
Number.EPSILON = Math.pow(2, -52); | |
} | |
if (Math.trunc === undefined) { | |
Math.trunc = function(v) { | |
return v < 0 ? Math.ceil(v) : Math.floor(v); | |
}; | |
} | |
var powers = [ | |
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, | |
1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, | |
1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22 | |
]; | |
var intpow10 = function(power) { | |
/* Not in lookup table */ | |
if (power < 0 || power > 22) { | |
return Math.pow(10, power); | |
} | |
return powers[power]; | |
}; | |
var isRound = function(num, decimalPlaces) { | |
//return decimalPlaces >= 0 && | |
// +num.toFixed(decimalPlaces) === num; | |
var p = intpow10(decimalPlaces); | |
return Math.round(num * p) / p === num; | |
}; | |
var decimalAdjust = function(type, num, decimalPlaces) { | |
if (type !== 'round' && isRound(num, decimalPlaces || 0)) | |
return num; | |
var p = intpow10(decimalPlaces || 0); | |
var n = (num * p) * (1 + Number.EPSILON); | |
return Math[type](n) / p; | |
}; | |
return { | |
// Decimal round (half away from zero) | |
round: function(num, decimalPlaces) { | |
return decimalAdjust('round', num, decimalPlaces); | |
}, | |
// Decimal ceil | |
ceil: function(num, decimalPlaces) { | |
return decimalAdjust('ceil', num, decimalPlaces); | |
}, | |
// Decimal floor | |
floor: function(num, decimalPlaces) { | |
return decimalAdjust('floor', num, decimalPlaces); | |
}, | |
// Decimal trunc | |
trunc: function(num, decimalPlaces) { | |
return decimalAdjust('trunc', num, decimalPlaces); | |
}, | |
// Format using fixed-point notation | |
toFixed: function(num, decimalPlaces) { | |
return decimalAdjust('round', num, decimalPlaces).toFixed(decimalPlaces); | |
} | |
}; | |
})(); | |
// Solution 2 | |
var DecimalPrecision22 = (function() { | |
if (Number.EPSILON === undefined) { | |
Number.EPSILON = Math.pow(2, -52); | |
} | |
if (Math.sign === undefined) { | |
Math.sign = function(x) { | |
return ((x > 0) - (x < 0)) || +x; | |
}; | |
} | |
var powers = [ | |
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, | |
1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, | |
1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22 | |
]; | |
var intpow10 = function(power) { | |
if (power < 0 || power > 22) { | |
return Math.pow(10, power); | |
} | |
return powers[power]; | |
}; | |
return { | |
// Decimal round (half away from zero) | |
round: function(num, decimalPlaces) { | |
var p = intpow10(decimalPlaces || 0); | |
var n = (num * p) * (1 + Number.EPSILON); | |
return Math.round(n) / p; | |
}, | |
// Decimal ceil | |
ceil: function(num, decimalPlaces) { | |
var p = intpow10(decimalPlaces || 0); | |
var n = (num * p) * (1 - Math.sign(num) * Number.EPSILON); | |
return Math.ceil(n) / p; | |
}, | |
// Decimal floor | |
floor: function(num, decimalPlaces) { | |
var p = intpow10(decimalPlaces || 0); | |
var n = (num * p) * (1 + Math.sign(num) * Number.EPSILON); | |
return Math.floor(n) / p; | |
}, | |
// Decimal trunc | |
trunc: function(num, decimalPlaces) { | |
//return num < 0 ? ceil(num, decimalPlaces) : floor(num, decimalPlaces); | |
return (num < 0 ? this.ceil : this.floor)(num, decimalPlaces); | |
}, | |
// Format using fixed-point notation | |
toFixed: function(num, decimalPlaces) { | |
return this.round(num, decimalPlaces).toFixed(decimalPlaces); | |
} | |
}; | |
})(); | |
// Solution 2 | |
var DecimalPrecision33 = (function() { | |
if (Number.EPSILON === undefined) { | |
Number.EPSILON = Math.pow(2, -52); | |
} | |
if (Math.sign === undefined) { | |
Math.sign = function(x) { | |
return ((x > 0) - (x < 0)) || +x; | |
}; | |
} | |
return { | |
// Decimal round (half away from zero) | |
round: function(num, decimalPlaces) { | |
var p = Math.pow(10, decimalPlaces || 0); | |
var n = (num * p) * (1 + Number.EPSILON); | |
return Math.round(n) / p; | |
}, | |
// Decimal ceil | |
ceil: function(num, decimalPlaces) { | |
var p = Math.pow(10, decimalPlaces || 0); | |
var n = (num * p) * (1 - Math.sign(num) * Number.EPSILON); | |
return Math.ceil(n) / p; | |
}, | |
// Decimal floor | |
floor: function(num, decimalPlaces) { | |
var p = Math.pow(10, decimalPlaces || 0); | |
var n = (num * p) * (1 + Math.sign(num) * Number.EPSILON); | |
return Math.floor(n) / p; | |
}, | |
// Decimal trunc | |
trunc: function(num, decimalPlaces) { | |
//return num < 0 ? ceil(num, decimalPlaces) : floor(num, decimalPlaces); | |
return (num < 0 ? this.ceil : this.floor)(num, decimalPlaces); | |
}, | |
// Format using fixed-point notation | |
toFixed: function(num, decimalPlaces) { | |
return this.round(num, decimalPlaces).toFixed(decimalPlaces); | |
} | |
}; | |
})(); | |
}; | |
suite.add("Solution 2 (Number.EPSILON)", function () { | |
/** Solution 2 (Number.EPSILON) **/ | |
DecimalPrecision2.round(0.5, 0); | |
DecimalPrecision2.round(-0.5, 0); | |
DecimalPrecision2.ceil(1e-8, 2); | |
DecimalPrecision2.floor(1e-8, 2); | |
DecimalPrecision2.round(5.12, 1); | |
DecimalPrecision2.round(-5.12, 1); | |
DecimalPrecision2.ceil(5.12, 1); | |
DecimalPrecision2.ceil(-5.12, 1); | |
DecimalPrecision2.floor(5.12, 1); | |
DecimalPrecision2.floor(-5.12, 1); | |
DecimalPrecision2.trunc(5.12, 1); | |
DecimalPrecision2.trunc(-5.12, 1); | |
DecimalPrecision2.round(1.005, 2); | |
DecimalPrecision2.round(39.425, 2); | |
DecimalPrecision2.round(-1.005, 2); | |
DecimalPrecision2.round(-39.425, 2); | |
DecimalPrecision2.ceil(9.130, 2); | |
DecimalPrecision2.ceil(65.180, 2); | |
DecimalPrecision2.ceil(-2.260, 2); | |
DecimalPrecision2.ceil(-18.150, 2); | |
DecimalPrecision2.floor(2.260, 2); | |
DecimalPrecision2.floor(18.150, 2); | |
DecimalPrecision2.floor(-9.130, 2); | |
DecimalPrecision2.floor(-65.180, 2); | |
DecimalPrecision2.trunc(2.260, 2); | |
DecimalPrecision2.trunc(18.150, 2); | |
DecimalPrecision2.trunc(-2.260, 2); | |
DecimalPrecision2.trunc(-18.150, 2); | |
DecimalPrecision2.round(1262.48, -1); | |
DecimalPrecision2.round(1262.48, -2); | |
}); | |
suite.add("Solution 22 (Number.EPSILON v2) **/", function () { | |
/** Solution 22 (Number.EPSILON v2) **/ | |
DecimalPrecision22.round(0.5, 0); | |
DecimalPrecision22.round(-0.5, 0); | |
DecimalPrecision22.ceil(1e-8, 2); | |
DecimalPrecision22.floor(1e-8, 2); | |
DecimalPrecision22.round(5.12, 1); | |
DecimalPrecision22.round(-5.12, 1); | |
DecimalPrecision22.ceil(5.12, 1); | |
DecimalPrecision22.ceil(-5.12, 1); | |
DecimalPrecision22.floor(5.12, 1); | |
DecimalPrecision22.floor(-5.12, 1); | |
DecimalPrecision22.trunc(5.12, 1); | |
DecimalPrecision22.trunc(-5.12, 1); | |
DecimalPrecision22.round(1.005, 2); | |
DecimalPrecision22.round(39.425, 2); | |
DecimalPrecision22.round(-1.005, 2); | |
DecimalPrecision22.round(-39.425, 2); | |
DecimalPrecision22.ceil(9.130, 2); | |
DecimalPrecision22.ceil(65.180, 2); | |
DecimalPrecision22.ceil(-2.260, 2); | |
DecimalPrecision22.ceil(-18.150, 2); | |
DecimalPrecision22.floor(2.260, 2); | |
DecimalPrecision22.floor(18.150, 2); | |
DecimalPrecision22.floor(-9.130, 2); | |
DecimalPrecision22.floor(-65.180, 2); | |
DecimalPrecision22.trunc(2.260, 2); | |
DecimalPrecision22.trunc(18.150, 2); | |
DecimalPrecision22.trunc(-2.260, 2); | |
DecimalPrecision22.trunc(-18.150, 2); | |
DecimalPrecision22.round(1262.48, -1); | |
DecimalPrecision22.round(1262.48, -2); | |
}); | |
suite.add("Solution 33 (Number.EPSILON v3) **/", function () { | |
/** Solution 33 (Number.EPSILON v3) **/ | |
DecimalPrecision33.round(0.5, 0); | |
DecimalPrecision33.round(-0.5, 0); | |
DecimalPrecision33.ceil(1e-8, 2); | |
DecimalPrecision33.floor(1e-8, 2); | |
DecimalPrecision33.round(5.12, 1); | |
DecimalPrecision33.round(-5.12, 1); | |
DecimalPrecision33.ceil(5.12, 1); | |
DecimalPrecision33.ceil(-5.12, 1); | |
DecimalPrecision33.floor(5.12, 1); | |
DecimalPrecision33.floor(-5.12, 1); | |
DecimalPrecision33.trunc(5.12, 1); | |
DecimalPrecision33.trunc(-5.12, 1); | |
DecimalPrecision33.round(1.005, 2); | |
DecimalPrecision33.round(39.425, 2); | |
DecimalPrecision33.round(-1.005, 2); | |
DecimalPrecision33.round(-39.425, 2); | |
DecimalPrecision33.ceil(9.130, 2); | |
DecimalPrecision33.ceil(65.180, 2); | |
DecimalPrecision33.ceil(-2.260, 2); | |
DecimalPrecision33.ceil(-18.150, 2); | |
DecimalPrecision33.floor(2.260, 2); | |
DecimalPrecision33.floor(18.150, 2); | |
DecimalPrecision33.floor(-9.130, 2); | |
DecimalPrecision33.floor(-65.180, 2); | |
DecimalPrecision33.trunc(2.260, 2); | |
DecimalPrecision33.trunc(18.150, 2); | |
DecimalPrecision33.trunc(-2.260, 2); | |
DecimalPrecision33.trunc(-18.150, 2); | |
DecimalPrecision33.round(1262.48, -1); | |
DecimalPrecision33.round(1262.48, -2); | |
}); | |
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("Decimal Rounding - Number.EPSILON vs Number.EPSILON v2 #jsbench"); | |
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