enumerable icon indicating copy to clipboard operation
enumerable copied to clipboard

mixing directly into an array?

Open matthewmueller opened this issue 13 years ago • 4 comments

It'd be really sweet to be able to do something like this:

var users = [];
enumerable(users);

var matt = {
  name : 'matt',
  age : 23
};

var jim = {
  name : 'jim',
  age : 42
};

users.push(matt, jim);

var names = users.map('name') // [ 'matt', 'jim' ]

matthewmueller avatar Nov 08 '12 23:11 matthewmueller

users = enumerable(users) will work, I do this in the tests a lot, returns an Enumerable with the array as .obj as a wrapper. the wrapper is for the same reason as Emitter, more efficient than doing a one-of mixin

tj avatar Nov 08 '12 23:11 tj

Hmm, do you happen to have any stats/links on performance differences?

In this example you'd have to do users.value().push(matt, jim); which is something found really annoying about working with backbone's collections.

matthewmueller avatar Nov 08 '12 23:11 matthewmueller

i havent bothered to benchmark it but it's a large difference considering you're adding one prop, or adding ~40 each time and v8 can't inline the lookups etc.

yeah it's definitely annoying, prototypes and inheritance are very flawed at best, we could maybe have a higher-level enumerable array thing that just hides the array inside but still has .push etc, kinda like https://github.com/component/collection/blob/master/index.js

tj avatar Nov 08 '12 23:11 tj

unfortunately with how js is structured you more or less cannot cleanly extend things, just a language-level flaw, but almost every language does the same dumb things :(

tj avatar Nov 08 '12 23:11 tj