Writing Fast, Memory-Efficient JavaScript
11–20 of 32 posts
Re: Writing Fast, Memory-Efficient JavaScript
#12To "delete" is appropriate for hash-like structures, i.e., key-value maps. The runtime can't optimize these anyway; they will always be "generic slow objects". Plus, delete is the only way to remove the key, otherwise your hash will only grow in size, and you'd store a lot of "null" values for no reason that you'd have to explicitly filter.
Re: Writing Fast, Memory-Efficient JavaScript
#13Not sure I'll write it anymore though, this seems to have most of what I was going to say!
EDIT: After reading the article in more details, there are a few things I think it misses. My focus is more on games though. I still might write another article.
Re: Writing Fast, Memory-Efficient JavaScript
#14The title should be "Writing JavaScript code that turns out to be fast and memory-efficient in the 2012 editions of JS engines". When discussing the efficiency of a language, especially an interpreted language, one must always take into account the peculiarities of the used VM. And be sure that today's optimisations may be "pessimizations" in the future (see the many Linux optimisations that are being removed in the…
I suppose what you're really getting at is that in JS we are subject to the whims of BOTH the compiler and the VM on the target browser?
Re: Writing Fast, Memory-Efficient JavaScript
#15 // Uses more memory:
Foo.prototype.someFunction = function () {
;[2, 5, 9].forEach(function (element, index, array) {
console.log("a[" + index + "] = " + element)
})
}
The function will be created every time the outer forEach loop is called. // Uses less memory:
Foo.prototype.logArrayElements = function (element, index, array) {
console.log("a[" + index + "] = " + element)
}
Foo.prototype.someFunction = function () {
;[2, 5, 9].forEach(this.logArrayElements)
}
Save your created functions for later to save memory.Re: Writing Fast, Memory-Efficient JavaScript
#16> Never use delete. Ever. ... This is horrible advice. There are plenty of things you want to delete, like references to elements when you don't need them anymore. A quick search of the Closure Library, jQuery, Backbone, Knockout, Angular.js, and ember.js found many uses of delete. Burn this article in a fire. As for the supposed performance difference, let's see a jsperf on that. Edit found one: http://jsperf.com/de…
I think the better take away might be: avoid changing the structure of 'hot' objects at runtime. JS engines will zero-in on these 'hot' objects and attempt to optimize access to it, a task which will be helped if object doesn't change in structure over its life-time. 'delete' will trigger such a structure change. By structure in context of JS, one should think inferrable structure like always setting .a to a Number t…
Re: Writing Fast, Memory-Efficient JavaScript
#17Avoiding nested anonymous functions is good for more reasons than listed in the article. The article lists closures holding references to the closed-over variables as being a source of wasted memory. Another point is the fact that anonymous variables require memory allocation each time they are created: // Uses more memory: Foo.prototype.someFunction = function () { ;[2, 5, 9].forEach(function (element, index, array)…
Re: Writing Fast, Memory-Efficient JavaScript
#18Avoiding nested anonymous functions is good for more reasons than listed in the article. The article lists closures holding references to the closed-over variables as being a source of wasted memory. Another point is the fact that anonymous variables require memory allocation each time they are created: // Uses more memory: Foo.prototype.someFunction = function () { ;[2, 5, 9].forEach(function (element, index, array)…
Where you have to be careful is if you have a function that's being called repeatedly and it contains a function instantiation -- that function will be reallocated each time.
[1,2,3].forEach(function(x) {
return (function(a, b) { return a * b })(x, 2)
})
Will repeatedly allocate that inner multiplication function, vs: mul = function(a, b) { return a * b }
;[1,2,3].forEach(function(x) {
return mul(x, 2)
})
Will only allocate that multiplication function once.Re: Writing Fast, Memory-Efficient JavaScript
#19V8 is not the only javascript engine.
Re: Writing Fast, Memory-Efficient JavaScript
#20AFAIK, JQuery HTML constructors use document fragments behind the scenes. See http://www.bennadel.com/blog/2281-jQuery-Appends-Multiple-El... . No need for explicit document.createDocumentFragment