javascriptperformancegarbage-collection

Are there any clear benefits from replacing a 'one time use' function with a property or object


In other words, with a code like this:

<script>
window['object'] = function(arg) {
  var myvar = 1;
  this.object = {};
  //assign new properties and do more stuff 
  this.object.x = arg?arg+1:myvar;//just one example for the context
}
//run function once and overwrite it with object
object(1);
</script>
  1. Is the function itself garbage collected?
  2. Does it cause an object reuse scenario which in itself would mitigate the cost of creating a new object?

This of course assumes the function requires contained local vars and/or argument dependencies, justifying having an actual function in order to set the object in the first place.


Solution

  • I am gonna close this question with the answer given on Twitter by Vyacheslav Egorov, which is self explanatory.

    there are no real performance benefits (ok, a single property can be reused, but that's it)