Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Faster(1.5 - 34 times) Array.unique speed. #1084

Closed
wants to merge 3 commits into from

2 participants

@arian
Owner

You're storing the values in an object, with the values as keys. Doing that will call valueOf() of each value. When the values are strings or numbers it's ok, however with other objects this probably won't work.

Also could you provide some http://jsperf.com

@cainrus

Updated. Tests: http://jsperf.com/mootools-array-uniq-vs-vanilla/2
Please check it. Do we need sugar or speed in core code? Need a framework for micro optimizations?

@arian
Owner

Very nice, however the cases I tried to explain, still don't work, see http://jsfiddle.net/EJX8n/ with your code and http://jsfiddle.net/EJX8n/1/ with the current code.

Faster is better, but what's more important is readability and API. Especially with these JS functions, which are usually not the bottleneck in your application (compared to DOM stuff).

@cainrus

agree

@cainrus cainrus closed this
@arian
Owner

Thanks for your effort though, much appreciated! If you see other possible improvements, please let us know :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 10 additions and 1 deletion.
  1. +10 −1 Source/Types/Array.Extras.js
View
11 Source/Types/Array.Extras.js
@@ -47,7 +47,16 @@ Array.implement({
},
unique: function(){
- return [].combine(this);
+ var h = {}, ret = [], arrLen = this.length, next=0, el, type,
+ needToCheckTypes = {'undefined': 1, 'boolean': 1, 'number': 1, 'string': 1};
+ while (arrLen--) {
+ el = this[arrLen];
+ type = typeof el;
+ if((needToCheckTypes[typeof el] || el === null) && h[el]) continue;
+ ret[next++] = el;
+ h[el] = 1;
+ }
+ return ret;
},
shuffle: function(){
Something went wrong with that request. Please try again.