Skip to content

Commit a09a696

Browse files
Merge pull request ryanmcdermott#124 from vsemozhetbyt/diff
make bad and old examples more comparable
2 parents d97805d + 54aed62 commit a09a696

File tree

1 file changed

+4
-15
lines changed

1 file changed

+4
-15
lines changed

README.md

+4-15
Original file line numberDiff line numberDiff line change
@@ -564,28 +564,17 @@ would be much better to just use ES2015/ES6 classes and simply extend the `Array
564564
**Bad:**
565565
```javascript
566566
Array.prototype.diff = function diff(comparisonArray) {
567-
const values = [];
568-
const hash = {};
569-
570-
for (const i of comparisonArray) {
571-
hash[i] = true;
572-
}
573-
574-
for (const i of this) {
575-
if (!hash[i]) {
576-
values.push(i);
577-
}
578-
}
579-
580-
return values;
567+
const hash = new Set(comparisonArray);
568+
return this.filter(elem => !hash.has(elem));
581569
};
582570
```
583571

584572
**Good:**
585573
```javascript
586574
class SuperArray extends Array {
587575
diff(comparisonArray) {
588-
return this.filter(elem => !comparisonArray.includes(elem));
576+
const hash = new Set(comparisonArray);
577+
return this.filter(elem => !hash.has(elem));
589578
}
590579
}
591580
```

0 commit comments

Comments
 (0)