From 54aed62267168d011b19776a16b6441445e61a00 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Thu, 12 Jan 2017 13:02:44 +0200 Subject: [PATCH] make bad and old examples more comparable Also address performance concerns. --- README.md | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 9504b922..516b232b 100644 --- a/README.md +++ b/README.md @@ -564,20 +564,8 @@ would be much better to just use ES2015/ES6 classes and simply extend the `Array **Bad:** ```javascript Array.prototype.diff = function diff(comparisonArray) { - const values = []; - const hash = {}; - - for (const i of comparisonArray) { - hash[i] = true; - } - - for (const i of this) { - if (!hash[i]) { - values.push(i); - } - } - - return values; + const hash = new Set(comparisonArray); + return this.filter(elem => !hash.has(elem)); }; ``` @@ -585,7 +573,8 @@ Array.prototype.diff = function diff(comparisonArray) { ```javascript class SuperArray extends Array { diff(comparisonArray) { - return this.filter(elem => !comparisonArray.includes(elem)); + const hash = new Set(comparisonArray); + return this.filter(elem => !hash.has(elem)); } } ```