Skip to content

Conversation

@H4ad
Copy link
Contributor

@H4ad H4ad commented Apr 22, 2023

Inspired by puleos/object-hash#122

perf: avoid toString when we know that the value is already a string

I did some tests and some values are fast by just leaving 'string' + value, other values are slow, so I only change the values that shows a significant improvement.

'with toString x 243,790,997 ops/sec ±4.95% (88 runs sampled)',
'without toString x 1,138,995,770 ops/sec ±0.12% (94 runs sampled)',
'bool: with toString x 222,584,175 ops/sec ±0.11% (97 runs sampled)',
'bool: without toString x 1,142,979,415 ops/sec ±0.46% (94 runs sampled)',
'number: with toString x 110,618,185 ops/sec ±0.48% (94 runs sampled)',
'number: without toString x 1,148,295,991 ops/sec ±0.05% (99 runs sampled)',
'url: with toString x 11,248,050 ops/sec ±1.76% (84 runs sampled)',
'url: without toString x 4,678,286 ops/sec ±0.95% (94 runs sampled)',
'bigint: with toString x 24,012,147 ops/sec ±1.97% (87 runs sampled)',
'bigint: without toString x 15,661,883 ops/sec ±2.36% (78 runs sampled)'

Benchmark: bench-to-string.js

@pi0 pi0 changed the title perf(object-hash): avoid toString when we know that the value is already a string perf(object-hash): avoid unnecessary .toString() on known values Apr 22, 2023
@pi0 pi0 merged commit 7e711dd into unjs:main Apr 22, 2023
@H4ad H4ad deleted the perf/avoid-to-string branch April 22, 2023 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants