Edoardo Luppi
07/16/2025, 9:55 AMBigInt
adoption in place of a custom Long
implementation will be very useful for kotlinx-io, or for the UUID/hex stdlib utilities, as they make use of longs quite often.Edoardo Luppi
07/16/2025, 5:33 PM@Benchmark
public fun bigInt(blackhole: Blackhole) {
var r = js("BigInt(0)")
for (i in 0..<100) {
r += js("BigInt(i)")
}
blackhole.consume(r.unsafeCast<Any>())
}
@Benchmark
public fun long(blackhole: Blackhole) {
var r = 0L
for (i in 0..<100) {
r += i.toLong()
}
blackhole.consume(r)
}
@Benchmark
public fun int(blackhole: Blackhole) {
var r = 0
for (i in 0..<100) {
r += i
}
blackhole.consume(r)
}
gives me this result:
Benchmark Mode Cnt Score Error Units
BigIntBenchmark.bigInt thrpt 4 1909.490 ± 85.321 ops/ms
BigIntBenchmark.long thrpt 4 1043.697 ± 4.642 ops/ms
<http://BigIntBenchmark.int|BigIntBenchmark.int> thrpt 4 13202.796 ± 176.814 ops/ms
If you start adding in bitwise operations, bigInt
will go down to match long
, but I think it's a rare situation.CLOVIS
07/17/2025, 7:48 AMEdoardo Luppi
07/17/2025, 7:49 AM>> << & |
. I bet those are not optimized by v8 or other engines.CLOVIS
07/17/2025, 7:49 AMCLOVIS
07/17/2025, 7:49 AMEdoardo Luppi
07/17/2025, 8:11 AMr += js("(BigInt(i) & BigInt(50))")
instead of the addition
Benchmark Mode Cnt Score Error Units
BigIntBenchmark.bigInt thrpt 4 1912.579 ± 17.503 ops/ms
<http://BigIntBenchmark.int|BigIntBenchmark.int> thrpt 4 13001.842 ± 235.835 ops/ms
BigIntBenchmark.long thrpt 4 671.619 ± 6.282 ops/ms
So BigInt
is still faster. HOWEVER, my observation is Long
will plateau at some point, while BigInt
will keep getting worse as more bitwise ops are added. With 10 ands, the two will be equal, with 20 BigInt
will be worse.Edoardo Luppi
07/17/2025, 8:14 AMBigInt
you'll also get a smaller bundle!