F16round Vs New Native Math.f16round
Introduction
The new Math.f16round
has just been added to web browsers baseline (April 2025). This new function is designed to round a float16
value to the nearest integer. In this article, we will compare the performance of f16round
vs new native Math.f16round
.
Performance Benchmark
To compare the performance of f16round
vs new native Math.f16round
, we ran a benchmark test. The test consisted of calling the function 100,000 times and measuring the time taken to perform the operation. We ran the test in Chrome and recorded the results.
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 |
As we can see from the results, f16round
is significantly faster than the new native Math.f16round
function. In fact, f16round
is up to 2.3x faster than the new native function.
Rounding Difference
In addition to the performance benchmark, we also compared the rounding behavior of f16round
vs new native Math.f16round
. We found that both functions produced identical float16
representations in 99.995% of cases. However, in 46 out of 1,000,000 results, both functions returned a value with the same absolute error distance, but biased in different directions.
# | error difference (absolute) | value | f16round | δ | (value error) | Math.f16round | δ | (value error) | AB% | ABΔ | err distance Δ |
---|---|---|---|---|---|---|---|---|---|---|---|
… | … | … | … | … | … | … | … | … | … | ||
#45628 | 0.000% | 26679.999479903603 |
26688 |
-0.030% | -8.00052009639694 |
26672 |
0.030% | 7.99947990360306 |
0.060% | 16 |
|
… | … | … | … | … | … | … | … | … | … | ||
#45993 | 0.000% | -59183.99838266712 |
-59200 |
-0.027% | 16.001617332876776 |
-59168 |
0.027% | -15.998382667123224 |
0.054% | -32 |
|
… | … | … | … | … | … | … | … | … | |||
#71590 | 0.000% | 48495.99984946958 |
48512 |
-0.033% | -16.00015053041716 |
48480 |
0.033% | 15.999849469582841 |
0.066% | 32 |
|
… | … | … | … | … | … | … | … | … | … | ||
#94772 | 0.000% | -35343.99924537736 |
-35360 |
-0.045% | 16.00075462264067 |
-35328 |
0.045% | -15.99924537735933 |
0.091% | -32 |
|
… | … | … | … | … | … | … | … | … | … | ||
#108886 | 0.000% | 37455.99896524107 |
37472 |
-0.043% | -16.001034758926835 |
37440 |
0.043% | 15.998965241073165 |
0.085% | 32 |
|
… | … | … | … | … | … | … | … | … | … | ||
#122404 | 0.000% | -11379.999721943781 |
-11384 |
-0.035% | 4.000278056219031 |
-11376 |
0.035% | -3.999721943780969 |
0.070% | -8 |
|
… | … | … | … | … | … | … | … | … | … | ||
#156758 | 0.000% | -48495.99870229898 |
-48512 |
-0.033% | 16.00129770101921 |
-48480 |
0.033% | -15.998702298980788 |
0.066% | -32 |
|
… | … | … | … | … | … | … | … | … | … | ||
#173958 | 0.000% | 58383.99985880201 |
58400 |
-0.027% | -16.00014119799016 |
58368 |
0.027% | 15.99985880200984 |
0.055% | 32 |
|
… | … | … | … | … | … | … | … | … | … | ||
#211086 | 0.000% | 63503.998548595286 |
63520 |
-0.025% | -16.001451404714317 |
63488 |
0.025% | 15.998548595285683 |
0.050% | 32 |
|
… | … | … | … | … | … | … | … | … | … | ||
#212242 | 0.000% | -36591.99888046086 |
-36608 |
-0.044% | 16.001119539141655 |
-36576 |
0.044% | -15.998880460858345 |
0.087% | -32 |
|
… | … | … | … | … | … | … | … | … | … | ||
#217329 | 0.000% | 26583.999344820 |
26592 |
-0.030% | -8.000655179173918 |
26576 |
0.030% | 7.999344820826082 |
0.060% | 16 |
|
… | … | … | … | … | … | … | … | … | … | ||
#218089 | 0.000% | -55087.9981567552 |
-55104 |
-0.029% | 16.001843244797783 |
-55072 |
0.029% | -15.998156755202217 |
0.058% | -32 |
|
… | … | … | … | … | … | … | … | … | … | ||
#243267 | 0.000% | 27879.999392021975 |
27888 |
-0.029% | -8.000607978025073 |
27872 |
0.029% | 7.999392021974927 |
0.057% | 16 |
|
… | … | … | … | … | … | … | … | … | … | ||
#263366 | 0.000% | 39183.99962272453 |
39200 |
-0.041% | -16.000377275471692 |
39168 |
0.041% | 15.999622724528308 |
0.082% | 32 |
|
… | … | … | … | … | … | … | … | … | … | ||
#295316 | 0.000% | 15843.999728316438 |
15848 |
-0.025% | -4.00027168356246 |
15840 |
0.025% | 3.9997283164375403 |
0.050% | 8 |
|
… | … | … | … | … | … | … | … | … | … | ||
#300629 | 0.000% | 52079.99983978948 |
52096 |
-0.031% | -16.000160210518516 |
52064 |
0.031% | 15.999839789481484 |
0.061% | 32 |
|
… | … | … | … |
Introduction
In our previous article, we compared the performance of f16round
vs new native Math.f16round
. We found that f16round
is significantly faster than the new native function. In this article, we will answer some frequently asked questions about the comparison.
Q: What is the main difference between f16round
and Math.f16round
?
A: The main difference between f16round
and Math.f16round
is their performance. f16round
is significantly faster than Math.f16round
. This is because f16round
is a custom implementation, while Math.f16round
is a native function.
Q: Why is f16round
faster than Math.f16round
?
A: f16round
is faster than Math.f16round
because it is a custom implementation that is optimized for performance. Math.f16round
, on the other hand, is a native function that is subject to the overhead of the JavaScript engine.
Q: What are the implications of f16round
being faster than Math.f16round
?
A: The implications of f16round
being faster than Math.f16round
are significant. If you are using Math.f16round
in your application, you may want to consider switching to f16round
to improve performance.
Q: Are there any other differences between f16round
and Math.f16round
?
A: Yes, there are other differences between f16round
and Math.f16round
. For example, f16round
has a different rounding behavior than Math.f16round
. However, this difference is relatively minor and should not affect most applications.
Q: Can I use f16round
in production?
A: Yes, you can use f16round
in production. However, you should be aware that f16round
is a custom implementation and may not be as widely supported as Math.f16round
.
Q: What are the benefits of using f16round
?
A: The benefits of using f16round
include improved performance and a custom implementation that is optimized for your specific use case.
Q: What are the drawbacks of using f16round
?
A: The drawbacks of using f16round
include the potential for reduced support and the need to maintain a custom implementation.
Q: Can I use Math.f16round
in production?
A: Yes, you can use Math.f16round
in production. However, you should be aware that Math.f16round
may not be as fast as f16round
.
Q: What are the benefits of using Math.f16round
?
A: The benefits of using Math.f16round
include wide support and a native implementation that is subject to the overhead of the JavaScript engine.
Q: What are the drawbacks of using Math.f16round
?
A: The drawbacks of using Math.f16round
include potential performance issues and a native implementation that may not be optimized for your specific use case.
Conclusion
In conclusion, f16round
and Math.f16round
are two different implementations of the f16round
function While f16round
is faster and has a custom implementation, Math.f16round
has wide support and a native implementation. The choice between f16round
and Math.f16round
will depend on your specific use case and requirements.