2 ms·
regarding your edit, you're exactly right, of course a for loop will be faster. Sometimes you really do need a function call though, in which case fast forEach
by phpnode 12y ago
regarding your edit, you're exactly right, of course a for loop will be faster. Sometimes you really do need a function call though, in which case fast forEach and map implementations become more useful.
The next step for fast.js are some sweet.js macros which will make writing for loops a bit nicer, because it's pretty painful to write this every time you want to iterate over an object:
var keys = Object.keys(obj),
length = keys.length,
key, i;
for (i = 0; i < length; i++) {
key = keys[i];
// ...
}
I'd rather write:
every key of obj {
// ...
}
and have that expanded at compile time.
Additionally there are some cases where you must use inline for loops (such as when slicing arguments objects, see https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments https://github.com/petkaantonov/bluebird/wiki/Optimization-k...) and a function call is not possible. These can also be addressed with sweet.js macros.
- throwaway_yy2Di 12y agoTo be fair, it's not obvious if you're not a JS expert: coming from some other language, you could naively assume that function call gets inlined, with no overhead.
- k__ 12y agoA few months ago, I tried some image processing with JS and the canvas object (big Arrays). They have a structure like image[pixel].color What I found was, always traversing the object structure was much slower than putting every pixel color as an argument in a function, that gets called every iteration. So I had the impression, reasonable simple function, like a greyscale filter, get inlined by engines like V8.
- warfangle 12y agoYou probably would have been better off with typed arrays and fragment shaders, if you were going for performance.
- k__ 12y agoAren't the arrays produced by a canvas object typed?
- warfangle 12y agoYep, but they only become performant when you treat them as such instead of doing naive e.g. multiplication. See: https://hacks.mozilla.org/2011/12/faster-canvas-pixel-manipulation-with-typed-arrays/ https://hacks.mozilla.org/2011/12/faster-canvas-pixel-manipu...
- bzbarsky 12y agoLike any other compiler, function calls sometimes get inlined. And sometimes not. Depending on compiler heuristics of various sorts.
- Excavator 12y agoNote that 'in' for (key in object) is for getting keys, in Firefox, meanwhile 'of' for (value of iterator) is for getting values out of an Iterator.
- olliej 12y agoMost of the array methods are explicitly defined in terms of indexing rather that iterators, so for(of) is incorrect. On the other hand indexing is still faster than iteration in the major implementations.
- Excavator 12y agoMy comment were intended as a direct reply to phpnode's code, 'ignoring' the rest of the thread. How do you mean when you state that for(of) is incorrect? Perhaps this 'example' clears something up? « for (value in {a:1,b:2,c:3,d:4,e:5}) console.log(value) » undefined "a" "b" "c" "d" "e" « for (value of {a:1,b:2,c:3,d:4,e:5}) console.log(value) × TypeError: ({a:1, b:2, c:3, d:4, e:5})['@@iterator'] is not a function
- phpnode 12y agothe only issue is i'd like to differentiate between iterating arrays and objects at the syntax level. Coffeescript uses `in` for arrays and `of` for objects, but I agree that becomes confusing. Open to other suggestions!
- Excavator 12y agoThat's a though one. I know I can't think of any that aren't either verbose or nondescript. But if Coffescript can walk away with its choice then I don't think you'll have anything to worry about.
- couchand 12y agoI've been spending some time thinking about CoffeeScript's choice for `in` and `of` and I think I have the answer: they should be more regular. One (probably `of`) should be used for the keys/indexes and the other (probably `in`) should be used for contents. These are legal CoffeeScript examples: for key, value of object if key of object for value in array if value in array for index, value of array if index of array The one that's missing is: for value in object if value in object