Javascript: Using function properties as "static" variables and optimization

142 Views Asked by At

One way to get "static" variables in a function (like the "static" keyword in C) in Javascript is to assign custom properties to the function object itself.

A few years ago, this caused problems with optimization in eg. V8 because when the function object's properties changed, the function had to be re-JIT-ted, etc.

Is this still the case, or is there a reliable way to benchmark this?

1

There are 1 best solutions below

1
jmrk On BEST ANSWER

V8 developer here. I don't remember that putting properties on function objects has ever caused problems. Do you have a source for that claim? Maybe it's a misunderstanding.

At any rate, these days you can definitely do that without issues.