nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
105.16k stars 28.49k forks source link

WeakRef has slow performance & degrades runtime performance #52444

Open btakita opened 2 months ago

btakita commented 2 months ago

Version

21.7.2

Platform

Linux logos 6.8.4-arch1-1 #1 SMP PREEMPT_DYNAMIC Fri, 05 Apr 2024 00:14:23 +0000 x86_64 GNU/Linux

Subsystem

V8?

What steps will reproduce the bug?

Install & run the following benchmark as a file.

https://github.com/btakita/weakref-benchmark

Notice that the bunjs is consistent. The Nodejs runtime has a dramatic degradation in performance as soon as WeakRef enters the benchmark.

How often does it reproduce? Is there a required condition?

Every time many WeakRef objects are instantiated.

What is the expected behavior? Why is that the expected behavior?

WeakRef should not cause performance degradation in the nodejs runtime.

What do you see instead?

WeakRef causes performance degradation in the nodejs runtime.

Additional information

I have not done any profiling yet.

btakita commented 2 months ago

I added a performance profile for node index.js.

btakita commented 2 months ago

I created a more focused benchmark on different strategies to achieve similar goals. WeakValueMap-performance-profile.txt is included.

Each benchmark includes 1000 set & 1000 get ops.

bun ./WeakValueMap.js
WeakMap x 33,849 ops/sec ±0.82% (95 runs sampled)
FinalizationRegistry x 21,919 ops/sec ±1.63% (93 runs sampled)
WeakRef x 68,214 ops/sec ±1.98% (91 runs sampled)
WeakValueMap x 7,224 ops/sec ±1.75% (86 runs sampled)
Fastest is WeakRef
node ./WeakValueMap.js
WeakMap x 20,373 ops/sec ±0.44% (96 runs sampled)
FinalizationRegistry x 13,921 ops/sec ±1.63% (88 runs sampled)
WeakRef x 1,517 ops/sec ±17.78% (19 runs sampled)
WeakValueMap x 722 ops/sec ±15.36% (81 runs sampled)
Fastest is WeakMap
deno run ./WeakValueMap.js
✅ Granted all read access.
WeakMap x 18,898 ops/sec ±0.52% (69 runs sampled)
FinalizationRegistry x 12,827 ops/sec ±2.11% (62 runs sampled)
WeakRef x 1,454 ops/sec ±21.63% (11 runs sampled)
WeakValueMap x 651 ops/sec ±14.85% (47 runs sampled)
Fastest is WeakMap
targos commented 2 months ago

I think there's nothing we can do about it in Node.js. I suggest you open an issue in the V8 tracker.

btakita commented 2 months ago

@targos Thank you for the reference. The V8 bug report is at https://issues.chromium.org/issues/333584632