Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Parse number to bigint
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser:
Chrome 131
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
bigint
6828433.0 Ops/sec
parts
7689870.0 Ops/sec
double
1811436.9 Ops/sec
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
/*your preparation JavaScript code goes here To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/ async function globalMeasureThatScriptPrepareFunction() { // This function is optional, feel free to remove it. // await someThing(); }
Tests:
bigint
/*When writing async/deferred tests, use `deferred.resolve()` to mark test as done*/ function numberToBigInt(num) { const str = num.toString(); const decimalIndex = str.indexOf('.'); const scale = decimalIndex === -1 ? 0 : str.length - decimalIndex - 1; const factor = Math.pow(10, scale); const scaledValue = Math.round(Math.abs(num) * factor); return { value: BigInt(scaledValue), exp: -scale }; } numberToBigInt(1230.1234);
parts
function numberToBigInt(num) { let intPart = Math.trunc(num); let fracPart = Math.abs(num - intPart); let scale = 0; while (fracPart !== 0 && scale < 20) { fracPart *= 10; intPart = intPart * 10 + Math.trunc(fracPart); fracPart = fracPart - Math.trunc(fracPart); scale++; } return { value: BigInt(Math.round(intPart)), exp: -scale }; } numberToBigInt(1230.1234);
double
function numberToBigInt(num) { const buffer = new ArrayBuffer(8); const view = new DataView(buffer); view.setFloat64(0, num); const sign = view.getUint8(0) >> 7; const exponent = ((view.getUint8(0) & 0x7F) << 4) | (view.getUint8(1) >> 4); const mantissa = BigInt(view.getUint32(4)) | (BigInt(view.getUint16(2) & 0x0FFF) << 32n); const bias = 1023; const scale = exponent - bias - 52; const value = mantissa | (1n << 52n); return { value: sign ? -value : value, exp: scale }; } numberToBigInt(1230.1234);
Прямое масштабирование с фиксированной точностью:
function numberToBigInt(num) { const PRECISION = 15; // Максимальная точность IEEE 754 const scale = BigInt(10 ** PRECISION); const scaledValue = BigInt(Math.round(num * Number(scale))); // Оптимизация: убираем лишние нули в конце let value = scaledValue; let exp = -PRECISION; while (value !== 0n && value % 10n === 0n) { value /= 10n; exp++; } return { value, exp }; } numberToBigInt(1230.1234);
Битовые операции (самый быстрый для целых чисел с автоматическим переходом на масштабирование):
function numberToBigInt(num) { // Быстрая проверка на целое число через битовые операции if ((num | 0) === num) { return { value: BigInt(num), exp: 0 }; } // Для дробных - используем масштабирование const scale = 1000000n; // 10^6 для достаточной точности const scaledValue = BigInt(Math.round(num * 1000000)); // Оптимизация хвостовых нулей let value = scaledValue; let exp = -6; while (value % 10n === 0n) { value /= 10n; exp++; } return { value, exp }; } numberToBigInt(1230.1234);
Комбинированный подход с кэшированием степеней:
function numberToBigInt(num) { // Кэш степеней 10 для оптимизации const POWERS = [1, 10, 100, 1000, 10000, 100000, 1000000]; // Определяем количество знаков после запятой const magnitude = Math.abs(num); let scale = 0; if (magnitude !== Math.floor(magnitude)) { const fraction = magnitude - Math.floor(magnitude); scale = Math.min(6, -Math.floor(Math.log10(fraction))); } const scaledValue = Math.round(num * POWERS[scale]); return { value: BigInt(scaledValue), exp: -scale }; } numberToBigInt(1230.1234);