yui/yui3 — pull request #939 by ziggyism (2013-06-28 17:31:34+00:00)
Title: Y.Array.dedupe performance and documentation

When dedupeing an array, `Y.Array.dedupe` uses an object, or hash, to store already visited strings. So, when iterating, `Object.prototype.hasOwnProperty` is used to make sure the string currently being visited will not collide with inherited properties of the hash (such as `constructor`, or `toString`).

This is good, however because of ES5, there is a better way. We can avoid calling the expensive method `Object.prototype.hasOwnProperty` N times, where N is the length of the array, by creating our hash using `Object.create(null)`.

`Object.create(null)` will inherit absolutely nothing, and so we avoid having to ever call `Object.prototype.hasOwnProperty`, which impacts performance, especially as the length of the array gets larger and larger.

I tested this idea here on a small array (of size 16): http://jsperf.com/y-array-dedupe
The results appear promising, even on a small array.

I have added:
* Feature detection of `Object.create`, so it can be used by `Y.Array.dedupe` when present.
* Unit test to ensure inherited properties (from `Object.prototype`) are not colliding with existing array items.

----------------------------

http://yuilibrary.com/yui/docs/api/classes/Array.html#method_dedupe

I also propose a change in the documentation of this method. It currently states this method can only be used on an array of strings. However, it is safe to say this method can also used on an array of numbers. The hashing of numbers will generally produce the same effect and results as if we were hashing strings.

Perhaps the documentation can be changed to state it can used on an array of **only strings** *or* an array of **only numbers**. An appropriate unit test demonstrating this idea has been added. Thoughts?