2 ms·
Every time you call createNew(), all the methods and all the properties are re-allocated. This is bad for performance and memory consumption. This is why proper
by jstsch 15y ago
Every time you call createNew(), all the methods and all the properties are re-allocated. This is bad for performance and memory consumption. This is why properties and methods should be added to the "prototype" of an object, so that they don't get re-allocated every time you want to create an instance.
Whilst this is true, isn't this something for the Javscript engine to optimize? And doesn't this (very common construct) already get optimized by modern engines? Guess I got a nice Sunday-evening experiment to set up...
Language-wise, I find the method the author describes to be very elegant. Everyone seems to write OOP-Javascript in their own way. And the new keywords in ECMAScript 5 aren't helping to resolve these inconsitencies...
- jstsch 15y agoI just gave this a go. In both Safari and Firefox I cannot see a performance difference. Good. I say give this method a go :) But maybe my experiment is flawed: https://gist.github.com/2340375 https://gist.github.com/2340375