sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Improve performance of js_util's `isJavaScriptArray`

Open mattrbeck opened this issue 1 year ago • 1 comments
trafficstars

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.

mattrbeck avatar Feb 15 '24 21:02 mattrbeck

  • summary: "The isJavaScriptArray function in js_util could be made faster by using Array.isArray instead of # instanceof Array."
  • triage to area-library (medium confidence)

(what's this?)

dart-github-bot avatar Feb 15 '24 21:02 dart-github-bot

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.

rakudrama avatar Feb 19 '24 19:02 rakudrama

Interesting read! Another reason to swap the implementation :)

mattrbeck avatar Feb 19 '24 19:02 mattrbeck

An additional change we can make here is exposing it on JSArray since it's a static method.

srujzs avatar Feb 23 '24 00:02 srujzs