1

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?

Seven Systems
  • 127
  • 3
  • 13

1 Answers1

1

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.

jmrk
  • 34,271
  • 7
  • 59
  • 74
  • Thanks. I had added a new syntax for "static" function-scoped variables to my own language that transpiles to JS a few years ago, and it essentially replaces @@foo by arguments.callee.foo. I wanted to be absolutely sure that this causes no issues and is optimization-safe, so I researched quite a bit and found at least one source that said that adding properties to a function would invalidate the compiled representation for V8. Glad to know it's safe! – Seven Systems Mar 15 '19 at 17:20