Comparing f16round vs new native Math.f16round
The new Math.f16round has just been added to web browsers baseline (April 2025) .
Just out of morbid curiosity I wanted to see how much faster the new native implementation is.
Performance benchmark
Surprisingly, native ran slower across the board.
percentile
f16round (100,000 calls)
Math.f16round (100,000 calls)
Δ%
p5
0.7 ms
1.6 ms
✅ tinybuf 2.3x faster
p25
0.8 ms
1.7 ms
✅ tinybuf 2.1x faster
median
0.9 ms
1.7 ms
✅ tinybuf 1.8x faster
p75
1.0 ms
1.8 ms
✅ tinybuf 1.8x faster
p95
1.4 ms
1.9 ms
✅ tinybuf 1.3x faster
Note
Tested in Chrome Version 135.0.7049.85
Benchmark code
const CALLS_PER_BATCH = 100_000 ;
const BATCH_COUNT = 100 ;
const values = Array . from ( { length : CALLS_PER_BATCH } , ( ) =>
Math . random ( ) * ( Math . random ( ) < 0.5 ? - 1 : 1 ) * 65504
) ;
function percentile ( sorted , p ) {
const i = Math . floor ( sorted . length * ( p / 100 ) ) ;
return sorted [ i ] ;
}
function benchmark ( label , fn ) {
const timings = [ ] ;
for ( let b = 0 ; b < BATCH_COUNT ; b ++ ) {
const start = performance . now ( ) ;
for ( let i = 0 ; i < CALLS_PER_BATCH ; i ++ ) {
fn ( values [ i ] ) ;
}
const end = performance . now ( ) ;
timings . push ( end - start ) ; // ms per 10k calls
}
timings . sort ( ( a , b ) => a - b ) ;
const points = [ 1 , 5 , 15 , 25 , 50 , 75 , 85 , 95 , 99 ] ;
console . log ( `${ label } (ms per 100,000 calls):` ) ;
for ( const p of points ) {
console . log ( ` p${ p . toString ( ) . padStart ( 2 ) } : ${ percentile ( timings , p ) . toFixed ( 3 ) } ` ) ;
}
}
// Run benchmarks
benchmark ( "f16round" , f16round ) ;
benchmark ( "Math.f16round" , Math . f16round ) ;
Rounding difference
Caution
Rounding bias: Both functions produced exact identical float16 representations in 99.994% results.
In 61 out of 1,000,000 results, both functions returned a value of near-identical error distance for each JavaScript float64 representation, but biased in different directions.
Where Tinybuf f16round() biased away from zero , and native Math.f16round() biased toward zero .
Error bias comparison:
mismatches: 61 mismatch % : 0.000061 average delta percent : - 0.0000001840
#
error difference
value
f16round
δ
(value error)
Math.f16round
δ
(value error)
AB%
ABΔ
#1802
🔻 0.000005857540%
64975.9980970024
64992
-0.025%
-16.001902997602883
64960
0.025%
15.998097002397117
0.049%
32
#17475
🔻 0.000008472052%
133.93749432637304
134
-0.047%
-0.06250567362695847
133.875
0.047%
0.06249432637304153
0.093%
0.125
#27504
🔻 0.000005351517%
937.7499749080772
938
-0.027%
-0.25002509192279376
937.5
0.027%
0.24997490807720624
0.053%
0.5
#84022
✅ -0.000000624565%
-132.06249958759167
-132.125
-0.047%
0.06250041240832616
-132
0.047%
-0.062499587591673844
0.095%
-0.125
#96656
✅ -0.000003583821%
-57583.99896814639
-57600
-0.028%
16.001031853607856
-57568
0.028%
-15.998968146392144
0.056%
-32
#106484
🔻 0.000001719804%
222.81249808403112
222.875
-0.028%
-0.06250191596888044
222.75
0.028%
0.06249808403111956
0.056%
0.125
#159354
🔻 0.000001640881%
74.90624938543871
74.9375
-0.042%
-0.03125061456128719
74.875
0.042%
0.031249385438712807
0.083%
0.0625
#167474
✅ -0.000005841653%
-88.90624740320263
-88.9375
-0.035%
0.03125259679737269
-88.875
0.035%
-0.03124740320262731
0.070%
-0.0625
#188683
🔻 0.000004278556%
239.3124948804409
239.375
-0.026%
-0.06250511955909133
239.25
0.026%
0.06249488044090867
0.052%
0.125
#229000
🔻 0.000002370383%
111.15624868258536
111.1875
-0.028%
-0.031251317414643154
111.125
0.028%
0.031248682585356846
0.056%
0.0625
#240577
✅ -0.000007320637%
-200.93749264504757
-201
-0.031%
0.0625073549524302
-200.875
0.031%
-0.062492645047569795
0.062%
-0.125
#248962
✅ -0.000005016296%
-243.93749388168663
-244
-0.026%
0.06250611831336528
-243.875
0.026%
-0.06249388168663472
0.051%
-0.125
#284345
✅ -0.000000884335%
-88.53124960854377
-88.5625
-0.035%
0.03125039145622566
-88.5
0.035%
-0.031249608543774343
0.071%
-0.0625
#301195
🔻 0.000003856436%
245.68749526260981
245.75
-0.025%
-0.06250473739018503
245.625
0.025%
0.06249526260981497
0.051%
0.125
#324731
🔻 0.000006076282%
205.06249376991263
205.125
-0.030%
-0.06250623008736511
205
0.030%
0.06249376991263489
0.061%
0.125
#380735
🔻 0.000002572228%
80.53124896427627
80.5625
-0.039%
-0.03125103572372723
80.5
0.039%
0.031248964276272773
0.078%
0.0625
#381721
✅ -0.000002753607%
-3258.999955129971
-3260
-0.031%
1.000044870028887
-3258
0.031%
-0.9999551299711129
0.061%
-2
#387265
✅ -0.000003198530%
-82.15624868610375
-82.1875
-0.038%
0.03125131389624869
-82.125
0.038%
-0.03124868610375131
0.076%
-0.0625
#406268
✅ -0.000001271347%
-160.68749897855216
-160.75
-0.039%
0.06250102144784364
-160.625
0.039%
-0.06249897855215636
0.078%
-0.125
#415602
🔻 0.000006118686%
53487.998363618644
53504
-0.030%
-16.001636381355638
53472
0.030%
15.998363618644362
0.060%
32
#420744
✅ -0.000003133470%
-10035.999842762501
-10040
-0.040%
4.00015723749857
-10032
0.040%
-3.99984276250143
0.080%
-8
#476565
🔻 0.000000318571%
7185.99998855375
7188
-0.028%
-2.000011446250028
7184
0.028%
1.9999885537499722
0.056%
4
#482953
✅ -0.000002832354%
-22071.999687421394
-22080
-0.036%
8.0003125786061
-22064
0.036%
-7.999687421393901
0.072%
-16
#513123
🔻 0.000010335055%
35.42187316956494
35.4375
-0.044%
-0.015626830435060413
35.40625
0.044%
0.015623169564939587
0.088%
0.03125
#528715
✅ -0.000002786001%
-31607.99955970045
-31616
-0.025%
8.000440299550974
-31600
0.025%
-7.999559700449026
0.051%
-16
#577560
🔻 0.000005663723%
13643.999613620832
13648
-0.029%
-4.000386379168049
13640
0.029%
3.9996136208319513
0.059%
8
#577781
✅ -0.000002584148%
-26151.999662096852
-26160
-0.031%
8.000337903147738
-26144
0.031%
-7.999662096852262
0.061%
-16
#582043
🔻 0.000000119462%
181.81249989140198
181.875
-0.034%
-0.06250010859801591
181.75
0.034%
0.06249989140198409
0.069%
0.125
#583715
🔻 0.000005239329%
58927.99845628411
58944
-0.027%
-16.001543715887237
58912
0.027%
15.998456284112763
0.054%
32
#605304
✅ -0.000001062334%
-7157.999961979075
-7160
-0.028%
2.000038020924876
-7156
0.028%
-1.999961979075124
0.056%
-4
#674150
🔻 0.000000954887%
18.53906241148648
18.546875
-0.042%
-0.007812588513520069
18.53125
0.042%
0.007812411486479931
0.084%
0.015625
#680311
✅ -0.000005258949%
-44111.99884008623
-44128
-0.036%
16.001159913772426
-44096
0.036%
-15.998840086227574
0.073%
-32
#686884
✅ -0.000004997706%
-52.57812368614997
-52.59375
-0.030%
0.015626313850027884
-52.5625
0.030%
-0.015623686149972116
0.059%
-0.03125
#695844
🔻 0.000000878038%
49359.99978330027
49376
-0.032%
-16.000216699729208
49344
0.032%
15.999783300270792
0.065%
32
#698861
✅ -0.000008323686%
-161.1874932916297
-161.25
-0.039%
0.06250670837030725
-161.125
0.039%
-0.06249329162969275
0.078%
-0.125
#702792
🔻 0.000000455713%
4.095703115667676
4.09765625
-0.048%
-0.0019531343323242467
4.09375
0.048%
0.0019531156676757533
0.095%
0.00390625
#722084
🔻 0.000001001549%
30.867187345424984
30.875
-0.025%
-0.007812654575015898
30.859375
0.025%
0.007812345424984102
0.051%
0.015625
#722113
✅ -0.000003994959%
-29.414061912460156
-29.421875
-0.027%
0.007813087539844332
-29.40625
0.027%
-0.007811912460155668
0.053%
-0.015625
#729953
🔻 0.000003675287%
22.75781208179259
22.765625
-0.034%
-0.007812918207410036
22.75
0.034%
0.007812081792589964
0.069%
0.015625
#753128
🔻 0.000004991556%
182.187495453005
182.25
-0.034%
-0.06250454699500096
182.125
0.034%
0.062495453004999035
0.069%
0.125
#755678
✅ -0.000000447069%
-108.21874975809376
-108.25
-0.029%
0.03125024190623549
-108.1875
0.029%
-0.031249758093764513
0.058%
-0.0625
#794643
✅ -0.000005591365%
-34703.99902978638
-34720
-0.046%
16.000970213623077
-34688
0.046%
-15.999029786376923
0.092%
-32
#839368
✅ -0.000002068722%
-228.31249763842467
-228.375
-0.027%
0.0625023615753264
-228.25
0.027%
-0.062497638424673596
0.055%
-0.125
#849578
✅ -0.000001509003%
-7.892578065450379
-7.89453125
-0.025%
0.001953184549621234
-7.890625
0.025%
-0.0019530654503787659
0.049%
-0.00390625
#861461
🔻 0.000005349619%
15811.999577059129
15816
-0.025%
-4.000422940871431
15808
0.025%
3.999577059128569
0.051%
8
#870578
✅ -0.000003734838%
-15147.99971712341
-15152
-0.026%
4.000282876590063
-15144
0.026%
-3.999717123409937
0.053%
-8
#870841
🔻 0.000003503228%
30759.99946120358
30768
-0.026%
-8.000538796419278
30752
0.026%
7.999461203580722
0.052%
16
#873690
🔻 0.000000870222%
146.43749936283405
146.5
-0.043%
-0.06250063716595378
146.375
0.043%
0.062499362834046224
0.085%
0.125
#876784
✅ -0.000003829990%
-249.56249522089092
-249.625
-0.025%
0.06250477910907648
-249.5
0.025%
-0.06249522089092352
0.050%
-0.125
#879356
✅ -0.000003252628%
-5677.9999076579015
-5680
-0.035%
2.000092342098469
-5676
0.035%
-1.999907657901531
0.070%
-4
#898188
✅ -0.000007659228%
-20711.999206810353
-20720
-0.039%
8.000793189647084
-20704
0.039%
-7.999206810352916
0.077%
-16
#904202
✅ -0.000004895500%
-53327.99869466404
-53344
-0.030%
16.001305335958023
-53312
0.030%
-15.998694664041977
0.060%
-32
#905957
🔻 0.000004470826%
58735.998687007974
58752
-0.027%
-16.001312992026214
58720
0.027%
15.998687007973786
0.054%
32
#908269
✅ -0.000003489676%
-39791.99930569404
-39808
-0.040%
16.000694305963407
-39776
0.040%
-15.999305694036593
0.080%
-32
#940929
✅ -0.000004794919%
-52719.9987360595
-52736
-0.030%
16.001263940503122
-52704
0.030%
-15.998736059496878
0.061%
-32
#956364
🔻 0.000003001084%
57359.99913928922
57376
-0.028%
-16.000860710781126
57344
0.028%
15.999139289218874
0.056%
32
#965336
✅ -0.000003711213%
-124.40624769150938
-124.4375
-0.025%
0.03125230849062177
-124.375
0.025%
-0.031247691509378228
0.050%
-0.0625
#970504
🔻 0.000008462484%
44207.99812945264
44224
-0.036%
-16.00187054736307
44192
0.036%
15.998129452636931
0.072%
32
#977917
🔻 0.000003176246%
213.81249660439408
213.875
-0.029%
-0.06250339560591556
213.75
0.029%
0.06249660439408444
0.058%
0.125
#980688
✅ -0.000005234306%
-94.40624752924425
-94.4375
-0.033%
0.031252470755745776
-94.375
0.033%
-0.031247529244254224
0.066%
-0.0625
#996014
✅ -0.000006319228%
-58191.99816135758
-58208
-0.027%
16.001838642419898
-58176
0.027%
-15.998161357580102
0.055%
-32
Benchmark code
const SAMPLES = 1_000_000 ;
const MAX = 65_504 ;
const REASONABLE = 250 ;
const values = Array . from ( { length : SAMPLES } , ( ) =>
Math . random ( ) * ( Math . random ( ) < 0.5 ? - 1 : 1 ) * ( Math . random ( ) < 0.5 ? REASONABLE : MAX )
) ;
function check_errors ( label , fnA , fnB ) {
let rows = [
`| # | error difference | value | f16round | δ | (value error) | Math.f16round | δ | (value error) | AB% | ABΔ |` ,
`| -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- |` ,
] ;
let mismatches = [ ] ;
for ( let i = 0 ; i < SAMPLES ; i ++ ) {
const aResult = fnA ( values [ i ] ) ;
const bResult = fnB ( values [ i ] ) ;
const value = values [ i ] ;
const errorASize = value - aResult ;
const errorAPercent = errorASize / value * 100 ;
const errorBSize = value - bResult ;
const errorBPercent = errorBSize / value * 100 ;
const abDelta = aResult - bResult ;
const abPercent = abDelta / value * 100 ;
const abAbsDelta = Math . abs ( errorASize ) - Math . abs ( errorBSize ) ;
const abAbsPercent = abAbsDelta / value * 100 ;
// Compare the outputs
const mismatch = aResult !== bResult ;
if ( mismatch ) {
mismatches . push ( abAbsPercent ) ;
rows . push ( `| #${ i } | ${ abAbsPercent < 0 ? '✅' : '🔻' } ${ abAbsPercent . toFixed ( 12 ) } % | \`${ value } \` | \`${ aResult } \` | ${ errorAPercent . toFixed ( 3 ) } % | \`${ errorASize } \` | \`${ bResult } \` | ${ errorBPercent . toFixed ( 3 ) } % | \`${ errorBSize } \` | ${ abPercent . toFixed ( 3 ) } % | \`${ abDelta } \` |` ) ;
}
}
const averageMismatchPercent = mismatches . reduce ( ( p , c ) => p + c , 0 ) / ( mismatches . length || 0 ) ;
console . log ( rows . join ( "\n" ) ) ;
console . log ( "mismatches:" , mismatches . length , "mismatch %:" , mismatches . length / SAMPLES , "average delta percent:" , averageMismatchPercent . toFixed ( 10 ) )
}
check_errors ( "f16round" , f16round , Math . f16round ) ;
Comparing
f16roundvs new nativeMath.f16roundThe new
Math.f16roundhas just been added to web browsers baseline (April 2025).Just out of morbid curiosity I wanted to see how much faster the new native implementation is.
Performance benchmark
Surprisingly, native ran slower across the board.
Note
Tested in Chrome Version 135.0.7049.85
Benchmark code
Rounding difference
Caution
Rounding bias: Both functions produced exact identical
float16representations in 99.994% results.In 61 out of 1,000,000 results, both functions returned a value of near-identical error distance for each JavaScript
float64representation, but biased in different directions.Where Tinybuf
f16round()biased away from zero, and nativeMath.f16round()biased toward zero.Error bias comparison:
64975.998097002464992-16.0019029976028836496015.99809700239711732133.93749432637304134-0.06250567362695847133.8750.062494326373041530.125937.7499749080772938-0.25002509192279376937.50.249974908077206240.5-132.06249958759167-132.1250.06250041240832616-132-0.062499587591673844-0.125-57583.99896814639-5760016.001031853607856-57568-15.998968146392144-32222.81249808403112222.875-0.06250191596888044222.750.062498084031119560.12574.9062493854387174.9375-0.0312506145612871974.8750.0312493854387128070.0625-88.90624740320263-88.93750.03125259679737269-88.875-0.03124740320262731-0.0625239.3124948804409239.375-0.06250511955909133239.250.062494880440908670.125111.15624868258536111.1875-0.031251317414643154111.1250.0312486825853568460.0625-200.93749264504757-2010.0625073549524302-200.875-0.062492645047569795-0.125-243.93749388168663-2440.06250611831336528-243.875-0.06249388168663472-0.125-88.53124960854377-88.56250.03125039145622566-88.5-0.031249608543774343-0.0625245.68749526260981245.75-0.06250473739018503245.6250.062495262609814970.125205.06249376991263205.125-0.062506230087365112050.062493769912634890.12580.5312489642762780.5625-0.0312510357237272380.50.0312489642762727730.0625-3258.999955129971-32601.000044870028887-3258-0.9999551299711129-2-82.15624868610375-82.18750.03125131389624869-82.125-0.03124868610375131-0.0625-160.68749897855216-160.750.06250102144784364-160.625-0.06249897855215636-0.12553487.99836361864453504-16.0016363813556385347215.99836361864436232-10035.999842762501-100404.00015723749857-10032-3.99984276250143-87185.999988553757188-2.00001144625002871841.99998855374997224-22071.999687421394-220808.0003125786061-22064-7.999687421393901-1635.4218731695649435.4375-0.01562683043506041335.406250.0156231695649395870.03125-31607.99955970045-316168.000440299550974-31600-7.999559700449026-1613643.99961362083213648-4.000386379168049136403.99961362083195138-26151.999662096852-261608.000337903147738-26144-7.999662096852262-16181.81249989140198181.875-0.06250010859801591181.750.062499891401984090.12558927.9984562841158944-16.0015437158872375891215.99845628411276332-7157.999961979075-71602.000038020924876-7156-1.999961979075124-418.5390624114864818.546875-0.00781258851352006918.531250.0078124114864799310.015625-44111.99884008623-4412816.001159913772426-44096-15.998840086227574-32-52.57812368614997-52.593750.015626313850027884-52.5625-0.015623686149972116-0.0312549359.9997833002749376-16.0002166997292084934415.99978330027079232-161.1874932916297-161.250.06250670837030725-161.125-0.06249329162969275-0.1254.0957031156676764.09765625-0.00195313433232424674.093750.00195311566767575330.0039062530.86718734542498430.875-0.00781265457501589830.8593750.0078123454249841020.015625-29.414061912460156-29.4218750.007813087539844332-29.40625-0.007811912460155668-0.01562522.7578120817925922.765625-0.00781291820741003622.750.0078120817925899640.015625182.187495453005182.25-0.06250454699500096182.1250.0624954530049990350.125-108.21874975809376-108.250.03125024190623549-108.1875-0.031249758093764513-0.0625-34703.99902978638-3472016.000970213623077-34688-15.999029786376923-32-228.31249763842467-228.3750.0625023615753264-228.25-0.062497638424673596-0.125-7.892578065450379-7.894531250.001953184549621234-7.890625-0.0019530654503787659-0.0039062515811.99957705912915816-4.000422940871431158083.9995770591285698-15147.99971712341-151524.000282876590063-15144-3.999717123409937-830759.9994612035830768-8.000538796419278307527.99946120358072216146.43749936283405146.5-0.06250063716595378146.3750.0624993628340462240.125-249.56249522089092-249.6250.06250477910907648-249.5-0.06249522089092352-0.125-5677.9999076579015-56802.000092342098469-5676-1.999907657901531-4-20711.999206810353-207208.000793189647084-20704-7.999206810352916-16-53327.99869466404-5334416.001305335958023-53312-15.998694664041977-3258735.99868700797458752-16.0013129920262145872015.99868700797378632-39791.99930569404-3980816.000694305963407-39776-15.999305694036593-32-52719.9987360595-5273616.001263940503122-52704-15.998736059496878-3257359.9991392892257376-16.0008607107811265734415.99913928921887432-124.40624769150938-124.43750.03125230849062177-124.375-0.031247691509378228-0.062544207.9981294526444224-16.001870547363074419215.99812945263693132213.81249660439408213.875-0.06250339560591556213.750.062496604394084440.125-94.40624752924425-94.43750.031252470755745776-94.375-0.031247529244254224-0.0625-58191.99816135758-5820816.001838642419898-58176-15.998161357580102-32Benchmark code