jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
const formatCurrency1 = (number, config) => {
const { style } = config ?? {};
const defaultStyle = style ?? 'currency';
const isFormattedAsCurrency = defaultStyle === 'currency';
return new Intl.NumberFormat(config.locale ?? config.appDefaultCurrencyLocale, {
style: defaultStyle,
currency: config.currency ?? isFormattedAsCurrency ? config.appDefaultCurrency : undefined,
currencyDisplay: config.currencyDisplay ?? isFormattedAsCurrency ? 'narrowSymbol' : undefined,
...config,
}).format(number);
};
const formatCurrency2 = (value, config) => {
const fixedValue = Number(value).toFixed(2);
const integerLength = fixedValue.length - 3;
const offset = integerLength % 3;
const hasTrailingZeros = fixedValue[fixedValue.length - 1] === '0' && fixedValue[fixedValue.length - 2] === '0';
let index = 0;
let formattedValue = '';
for (const char of fixedValue) {
if (index < integerLength) {
if (index > 0 && (index - offset) % 3 === 0) {
formattedValue += '’';
}
formattedValue += char;
} else {
if (
!(
(config.trailingZeroDisplay === 'stripIfInteger' && hasTrailingZeros) ||
(config.cutFractionAfterIntegerDigits && integerLength >= config.cutFractionAfterIntegerDigits)
)
) {
formattedValue += char;
}
}
index += 1;
}
if (config.currency) {
return `${config.currency} ${formattedValue}`;
}
return formattedValue;
};
Ready to run.
Test | Ops/sec | |
---|---|---|
formatCurrency1 |
| ready |
formatCurrency2 |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.