From e4145deeb7a72ac321113fde672e1b499ecb0d2d Mon Sep 17 00:00:00 2001 From: Mario Vavti Date: Wed, 18 Nov 2015 23:50:42 +0100 Subject: some events refacturing --- library/moment/benchmarks/zeroFill.js | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 library/moment/benchmarks/zeroFill.js (limited to 'library/moment/benchmarks/zeroFill.js') diff --git a/library/moment/benchmarks/zeroFill.js b/library/moment/benchmarks/zeroFill.js new file mode 100644 index 000000000..9ce76ee98 --- /dev/null +++ b/library/moment/benchmarks/zeroFill.js @@ -0,0 +1,43 @@ +var Benchmark = require('benchmark'); + +module.exports = { + name: 'zeroFill', + tests: { + zeroFillMath: { + setup: function() { + var zeroFillMath = function(number, targetLength, forceSign) { + var absNumber = '' + Math.abs(number), + zerosToFill = targetLength - absNumber.length, + sign = number >= 0; + return (sign ? (forceSign ? '+' : '') : '-') + + Math.pow(10, Math.max(0, zerosToFill)).toString().substr(1) + absNumber; + } + }, + fn: function() { + zeroFillMath(Math.random() * 1e5 | 0, 5); + zeroFillMath(Math.random() * 1e5 | 0, 10); + zeroFillMath(Math.random() * 1e10 | 0, 20); + }, + async: true + }, + zeroFillWhile: { + setup: function() { + var zeroFillWhile = function(number, targetLength, forceSign) { + var output = '' + Math.abs(number), + sign = number >= 0; + + while (output.length < targetLength) { + output = '0' + output; + } + return (sign ? (forceSign ? '+' : '') : '-') + output; + } + }, + fn: function() { + zeroFillWhile(Math.random() * 1e5 | 0, 5); + zeroFillWhile(Math.random() * 1e5 | 0, 10); + zeroFillWhile(Math.random() * 1e10 | 0, 20); + }, + async: true + } + } +}; -- cgit v1.2.3