Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Map shim with _.uniq #2108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 51 additions & 10 deletions underscore.js
Expand Up @@ -131,6 +131,52 @@
return typeof length == 'number' && length >= 0 && length <= MAX_ARRAY_INDEX;
};

// A small Map shim, to support the most basic use cases.
var MapShim = function() {
this._values = [];
this._keys = [];
this._primitives = nativeCreate && nativeCreate(null);
this.size = 0;

this._lastKey = void 0;
this._lastIndex = -1;
};
var keyer = function(value) {
return typeof value === 'string' ? 's' + value : value;
};
MapShim.prototype.get = function(key) {
if (this.has(key)) return this._values[this._lastIndex];
};
MapShim.prototype.has = function(key) {
var index = this._indexOf(key);
return index >= 0 && index < this.size;
};
MapShim.prototype.set = function(key, value) {
var index = this._indexOf(key);

this._values[index] = value;
if (index >= this.size) {
this.size = index + 1;
this._keys[index] = key;
if (this._primitives && !_.isObject(key)) this._primitives[keyer(key)] = index;
}
};
MapShim.prototype._indexOf = function(key) {
var index = this._lastIndex;
if (this._lastKey === key && index >= 0) return index;
if (this._primitives && !_.isObject(key)) {
index = this._primitives[keyer(key)];
} else {
index = _.indexOf(this._keys, key);
}
if (index === -1 || index === void 0) index = this.size;

this._lastKey = key;
this._lastIndex = index;
return index;
};


// Collection Functions
// --------------------

Expand Down Expand Up @@ -518,21 +564,16 @@
isSorted = false;
}
if (iteratee != null) iteratee = cb(iteratee, context);
var result = [];
var seen = [];
var seen = new MapShim();
var result = seen._values;
for (var i = 0, length = array.length; i < length; i++) {
var value = array[i],
computed = iteratee ? iteratee(value, i, array) : value;
if (isSorted) {
if (!i || seen !== computed) result.push(value);
if (seen !== computed) result.push(value);
seen = computed;
} else if (iteratee) {
if (!_.contains(seen, computed)) {
seen.push(computed);
result.push(value);
}
} else if (!_.contains(result, value)) {
result.push(value);
} else if (!seen.has(computed)) {
seen.set(computed, value);
}
}
return result;
Expand Down