You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The main point is that len is so fast in Go I would consider it "free", and would never blink an eye using it. In GopherJS, it can have dire consequences on the performance of code as a map grows in size. I find many of our uses of len look like if len(myMap) > 0 { doStuff}. Even if I change those to if myMap ~= nil, at some point iteration may happen, and then the cost must be paid. I believe it is unavoidable to use range to iterate a map.
I'm not entirely sure how to approach this, but it seems like implementing go maps as a javascript map could be a way to go, instead of a plain old javascript object. I thought briefly about implementing bookkeeping when adding or deleting an element to the map, but that would only solve for the len() case and leave range.
I'm not sure if there are other cases to handle (it looks like $keys is used a few places in reflect.go and reflectlite.go, but I don't understand that code right now.
My desires:
Make it fast to determine the length of a map, regardless of size.
Make it fast to get the keys of a map (range).
The text was updated successfully, but these errors were encountered:
Yes, using ES 2015 Map type is likely the best solution, and I would venture a guess that it can improve performance overall. I see you already started prototyping in #1136, I'm looking forwards to seeing the results.
reflect and reflectlite packages are some of the more arcane corners of GopherJS and I can't say I understand it fully, but if you have questions feel free to ask, and we will try to help you :)
I'm using go 1.17.3 and gopherjs 1.17.1+go1.17.3
I have noticed that calling
len()
orrange
on a map is implemented in JS to call length on$keys
.var $keys = function(m) { return m ? Object.keys(m) : []; };
This appears to scale linearly as a map grows in size.
Consider the following benchmarks:
In Go:
In Gopherjs:
So, the map size of 1000000 may be considered ridiculously big, so, I'll back off to 1000.
The main point is that len is so fast in Go I would consider it "free", and would never blink an eye using it. In GopherJS, it can have dire consequences on the performance of code as a map grows in size. I find many of our uses of len look like
if len(myMap) > 0 { doStuff}
. Even if I change those toif myMap ~= nil
, at some point iteration may happen, and then the cost must be paid. I believe it is unavoidable to use range to iterate a map.I'm not entirely sure how to approach this, but it seems like implementing go maps as a javascript map could be a way to go, instead of a plain old javascript object. I thought briefly about implementing bookkeeping when adding or deleting an element to the map, but that would only solve for the
len()
case and leaverange
.I'm not sure if there are other cases to handle (it looks like $keys is used a few places in
reflect.go
andreflectlite.go
, but I don't understand that code right now.My desires:
The text was updated successfully, but these errors were encountered: