sdk
sdk copied to clipboard
Improve performance of js_util's `isJavaScriptArray`
I know that new js-interop is a current priority which may put libraries like js_util on the back burner, but from my reading this seems like an easy win.
isJavaScriptArray currently expands to # instanceof Array (1 and 2).
Testing on my machine on Chrome 120, it seems Array.isArray is ~7x faster than instanceof Array (https://jsperf.app/wakace). Even though it's trivial to create a binding to Array.isArray using js_util, I propose swapping the implementation, assuming performance is also an improvement in other modern browsers.
I'm unaware of any behavioral differences between the two. I'm guessing instanceof was the preferred approach before for performance reasons. I found a couple 7-10 year-old stackoverflow comments stating they measured performance to be better using instanceof, but it seems that's no longer the case.
- summary: "The
isJavaScriptArrayfunction injs_utilcould be made faster by usingArray.isArrayinstead of# instanceof Array." - triage to
area-library(medium confidence)
This is a good idea.
Code within the Dart runtime type support already uses Array.isArray to make Array implement Dart List.
There is a difference - Array.isArray(x) will recognize Arrays from other windows, x instanceof Array will not.
More here.
Interesting read! Another reason to swap the implementation :)
An additional change we can make here is exposing it on JSArray since it's a static method.