3 ms·
Turning an object into an array isn't a nice way to do things IMHO. Maybe runtimes optimize for these cases, or maybe they create completely new objects every t
by bumblebird 17y ago
Turning an object into an array isn't a nice way to do things IMHO. Maybe runtimes optimize for these cases, or maybe they create completely new objects every time and you get fantastic object churn.
Yes it works well, but it's not really necessary. It's inefficient wasted effort.
I guess it's fun to make lovely APIs, but that doesn't help people who just want their js to run fast.
- jacobolus 17y ago> Turning an object into an array isn't a nice way to do things This doesn’t happen. The object is unchanged when you call array.slice. > Maybe runtimes optimize Only insofar as the built-in array methods are optimized anyway. > maybe they create completely new objects No, they don’t. Or rather, yes, array.slice creates a new object to put the slice in, but only because that is part of its declared API. > Yes it works well, but it's not really necessary. What’s the alternative? > It's inefficient wasted effort. Inefficient how? If you time it, it’s faster other ways of accomplishing the same thing. Whose wasted effort? It certainly saves me time as a JavaScript developer. * * * To be honest, I think that arguments should just be an array (then we wouldn’t have any of this trouble), but I assume that’s not possible for the language at this point.
- bumblebird 17y ago>> This doesn’t happen. The object is unchanged when you call array.slice. You're wrong. Why don't you check instead of reading API docs or saying what you think should happen? It obviously is changed, and is a new object: Array.prototype.slice.call({0:"hello",1:"world",foo:"bar",length:2}); Results in ["hello","world"] - the result is a completely new object - inefficient wasted effort. Both in cpu and memory. Offtopic again, but here's an interesting way to make a new N element array, initialized with undefineds... Array.prototype.slice.call({length:100}) = [undefined, undefined, undefined........ undefined] Array.prototype.slice.call({length:10000000}) takes almost 1 second to run on my MBP while it's creating that new array. >> What’s the alternative? As I say, just write closures... onclick = function(c){return function(){c.doSomething()}}(this); It's not much more typing, and is more general. I agree arguments would be nicer if it was a true array though.
- bumblebird 17y agoInstead of downmodding, why not explain why you disagree with the facts I present above?