calculate amounts or strings for rendering price (v3)

Revision 3 of this benchmark created on


Preparation HTML

<p id="price"></p>

Setup

const numberFormat = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" });

window.RENDER_FORMATTED_PRICE = async function(data) {

	numberFormat.format(data);
	const formattedPrice = numberFormat.format(data);
 document.getElementById('price').textContent = formattedPrice
 await console.log(`Rendering`, data, formattedPrice);
}

Test runner

Ready to run.

Testing in
TestOps/sec
Represent directly
const dataAsNumber = 92.99
const formattedPrice = numberFormat.format(dataAsNumber);
console.log(formattedPrice, 'number')

document.getElementById('price').textContent = formattedPrice
ready
Represent with transform from string and parsefloat
const dataAsString = '92.99'
const formattedPrice = numberFormat.format(parseFloat(dataAsString));
console.log(formattedPrice, 'string')

ready
Represent without parsefloat just strings
const dataAsString = '92.99'
const formattedPrice = numberFormat.format(dataAsString);
console.log(formattedPrice, 'string no parsefloat')
document.getElementById('price').textContent = formattedPrice
ready
Represents with transforms of units nanos as nums
const dataAsUnitsNanosNums = { units: 92, nanos: 99 }
const formattedPrice = numberFormat.format(`${dataAsUnitsNanosNums.units}.${dataAsUnitsNanosNums.nanos}`);
console.log(formattedPrice, 'units/nanos as nums')
document.getElementById('price').textContent = formattedPrice
ready
Represent with transform from units nanos
const dataAsUnitsNanos = { units: BigInt(92), nanos: BigInt(99 * 1e6)}
const formattedPrice = numberFormat.format(Number(dataAsUnitsNanos.units) + (Number(dataAsUnitsNanos.nanos) / 1e6));
console.log(formattedPrice, 'units/nanos')
document.getElementById('price').textContent = formattedPrice
ready
Represent directly (async)
await window.RENDER_FORMATTED_PRICE(92.99)
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.