Skip to content

Commit

Permalink
Merge pull request #15949 from bekzod/use-set-for-unique
Browse files Browse the repository at this point in the history
[CLEANUP canary] use `Set` for uniqBy and uniq
  • Loading branch information
rwjblue authored Dec 9, 2017
2 parents 46a964c + 6e5cb66 commit a3b3b6c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
14 changes: 7 additions & 7 deletions packages/ember-runtime/lib/computed/reduce_computed_macros.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/**
@module @ember/object
*/

import { guidFor } from 'ember-utils';
import { assert } from 'ember-debug';
import {
get,
Expand Down Expand Up @@ -407,12 +405,14 @@ export function filterBy(dependentKey, propertyKey, value) {
export function uniq(...args) {
return multiArrayMacro(args, function(dependentKeys) {
let uniq = emberA();
let seen = new Set();

dependentKeys.forEach(dependentKey => {
let value = get(this, dependentKey);
if (isArray(value)) {
value.forEach(item => {
if (uniq.indexOf(item) === -1) {
if (!seen.has(item)) {
seen.add(item);
uniq.push(item);
}
});
Expand Down Expand Up @@ -461,13 +461,13 @@ export function uniqBy(dependentKey, propertyKey) {

let cp = new ComputedProperty(function() {
let uniq = emberA();
let seen = Object.create(null);
let list = get(this, dependentKey);
if (isArray(list)) {
let seen = new Set();
list.forEach(item => {
let guid = guidFor(get(item, propertyKey));
if (!(guid in seen)) {
seen[guid] = true;
let val = get(item, propertyKey);
if (!seen.has(val)) {
seen.add(val);
uniq.push(item);
}
});
Expand Down
17 changes: 9 additions & 8 deletions packages/ember-runtime/lib/mixins/enumerable.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// HELPERS
//

import { guidFor } from 'ember-utils';
import {
get,
set,
Expand Down Expand Up @@ -811,9 +810,11 @@ const Enumerable = Mixin.create({
uniq() {
let ret = emberA();

this.forEach(k => {
if (ret.indexOf(k) < 0) {
ret.push(k);
let seen = new Set();
this.forEach(item => {
if (!seen.has(item)) {
seen.add(item);
ret.push(item);
}
});

Expand Down Expand Up @@ -1066,12 +1067,12 @@ const Enumerable = Mixin.create({

uniqBy(key) {
let ret = emberA();
let seen = Object.create(null);
let seen = new Set();

this.forEach((item) => {
let guid = guidFor(get(item, key));
if (!(guid in seen)) {
seen[guid] = true;
let val = get(item, key);
if (!seen.has(val)) {
seen.add(val);
ret.push(item);
}
});
Expand Down

0 comments on commit a3b3b6c

Please sign in to comment.