Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #24441
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Difference between two arrays |
| Date | 2014-05-27 17:41 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <73754615.KVtT6colLE@PointedEars.de> (permalink) |
| References | <z_ydnZKbtslwbR7OnZ2dnUVZ_rGdnZ2d@westnet.com.au> <0.e9255226716292b2ac8f.20140527134102BST.87lhtne6n5.fsf@bsb.me.uk> |
Ben Bacarisse wrote:
> Andrew Poulos <ap_prog@hotmail.com> writes:
>> If I have two "simple" arrays and I need to create a third array of
>> elements that are only in one of the arrays. I found this
>>
>> Array.prototype.difference = function (a) {
>> return this.filter(function (i) {
>> return !(a.indexOf(i) > -1);
>> });
>> };
>
> […]
> Anyway, the call constructs an array the includes only those elements of
> "this" that can't be found in "a".
>
> Depending on lots of unknowns, it may be faster to invert the arrays.
> I.e. to show the presence of element x by setting a[x] to some specific
> value, rather than have some index i at which a[i] === x.
You can not simply invert arrays in ECMAScript implementations. Arrays are
objects, therefore keys are properties of an object. Values whose string
representation is the same as a property of the object can overwrite built-
in properties.
> This makes testing for membership fast, but at the expense of other
> things. In particular, unless the implementation is clever, a very wide
> range of values can result is very large inverted arrays.
If the values can be unambiguously converted to String, using Object
instances with empty prototype chain is recommended. The property name
would be the string representation of the value. Otherwise, a Map
implementation such as those in JSX:map.js could/should be used.
> The symmetric difference then looks like this:
>
> Array.prototype.symDiff = function (a) {
> var count = [];
> this.forEach(function (e) { count[e] = 1; });
> a.forEach(function (e) { count[e] = (count[e] || 0) | 2; });
> var result = [];
> count.forEach(function (e, i) { if (e !== 3) result.push(i); });
In most cases, you want to use a “for … in …” loop instead of
Array.prototype.forEach(), particularly where speed is of the essence. Not
only can you save several function calls this way, you can also avoid
unnecessary iterations with sparse arrays. It should be tested before
whether array indexes are enumerable (per ES and in recent implementations
they are), and potentially augmented prototypes have to be considered.
Also, forEach() operates *only* on array indexes. Therefore, the approach
above will fail if values are not array indexes.
<http://stackoverflow.com/a/17000264/855543>
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Difference between two arrays Andrew Poulos <ap_prog@hotmail.com> - 2014-05-27 11:55 +1000
Re: Difference between two arrays Denis McMahon <denismfmcmahon@gmail.com> - 2014-05-27 05:58 +0000
Re: Difference between two arrays John C <rescattered@gmail.com> - 2014-05-27 03:34 -0700
Re: Difference between two arrays Spamless <Spamless@Nil.nil> - 2014-05-31 06:35 -0500
Re: Difference between two arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-31 14:57 +0200
Re: Difference between two arrays Spamless <Spamless@Nil.nil> - 2014-06-01 03:43 -0500
Re: Difference between two arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-01 14:01 +0200
Re: Difference between two arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-01 16:53 +0200
Re: Difference between two arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-05-27 13:41 +0100
Re: Difference between two arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-27 17:41 +0200
Re: Difference between two arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-27 17:49 +0200
Re: Difference between two arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-05-27 17:34 +0100
Re: Difference between two arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-27 19:33 +0200
Re: Difference between two arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-05-27 20:03 +0100
Re: Difference between two arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-05-27 17:16 +0100
Re: Difference between two arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-27 19:29 +0200
Re: Difference between two arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-27 17:21 +0200
Re: Difference between two arrays Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-05-28 18:22 +0100
Re: Difference between two arrays Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-05-30 22:23 +0100
Re: Difference between two arrays "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-05-31 14:59 -0700
Re: Difference between two arrays "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-06-01 00:50 +0200
Re: Difference between two arrays "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-05-31 16:17 -0700
Re: Difference between two arrays "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-06-01 10:49 +0200
Re: Difference between two arrays "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-06-01 13:37 -0700
Re: Difference between two arrays Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-06-01 18:56 +0100
csiph-web