Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #29872
| Path | csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: Bubblesort |
| Date | Wed, 09 Mar 2016 22:04:44 +0100 |
| Organization | PointedEars Software (PES) |
| Lines | 117 |
| Message-ID | <1614595.1D9Jq9LFrv@PointedEars.de> (permalink) |
| References | <0d68ee6d-8a80-439e-ae04-17743584e2e4@googlegroups.com> <nbbp99$fed$1@dont-email.me> <XnsA5C17A5C66BA1eejj99@194.109.6.166> <086a7397-459b-46e3-b83c-cfc7edf4188a@googlegroups.com> <7b1ee9db-4732-48e1-be7e-37ba62db4c6e@googlegroups.com> <nbc50g$44d$1@news.albasani.net> <2815605.oGPRxZOVvt@PointedEars.de> <nbnur4$4tr$1@news.albasani.net> |
| Reply-To | Thomas 'PointedEars' Lahn <cljs@PointedEars.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Content-Transfer-Encoding | 8Bit |
| X-Trace | solani.org 1457557488 31802 eJwFwYkBgDAIA8CVGhoex5EI+4/gnd9AKBke9PVNUCJqxrZP1dTD5G2edxRpbh/PCEKHGeYHFVIQxg== (9 Mar 2016 21:04:48 GMT) |
| X-Complaints-To | abuse@news.solani.org |
| NNTP-Posting-Date | Wed, 9 Mar 2016 21:04:48 +0000 (UTC) |
| User-Agent | KNode/4.14.2 |
| X-User-ID | eJwFwQcBACAIBMBKMp4RB0T6R/AOYmTX1WCKxYowcMn2mnuFcfkkFyU8KPOA+43rmZecXXJmUBUdWph++wExABVu |
| Cancel-Lock | sha1:SpWAF3XJ2SfSt2QD+LTCZKLcgn8= |
| X-NNTP-Posting-Host | eJwFwQkRwEAIBDBL7MHyyGGg+JfQhOrwCXO68XgVt/gEguk3W+zUNlhFb0Et9IwtLy49U+YHFFYQcg== |
| Xref | csiph.com comp.lang.javascript:29872 |
Show key headers only | View raw
Stefan Weiss wrote:
> On 03/08/2016 23:35, Thomas 'PointedEars' Lahn wrote:
>> Stefan Weiss wrote:
>>> Alternatively, you can sort string array elements using a numeric
>>> comparison function:
>>>
>>> arr[x].nodelinks.sort((a, b) => a - b);
>>>
>>> Or in a more verbose form (but with better UA compatibility):
>>>
>>> function compareNumeric (a, b)
>>> {
>>> return a - b;
>>> }
>>>
>>> arr[x].nodelinks.sort(compareNumeric);
>>
>> For reasonable backwards-compatibility, the function declaration is
>> unnecessary/unwise, unless the comparator is declared locally *and* used
>> locally more than once (in which case a function declaration/variable
>> initialization saves memory).
>
> In this case, sort() is used in a loop.
Then a local variable declaration with initialization (see below) or a local
function declaration is appropriate, unless the function makes an closure
over a name with a loop-variant value.
>> Function expressions are backwards-compatible down to JavaScript 1.3 and
>> JScript 5.0 at least (Netscape Navigator 4.06+, Internet Explorer 5.0),
>> therefore marked “safe” in the ECMAScript Support Matrix since its
>> inception in 2005 (IIRC).
>
> I didn't mean to say that function declarations were more compatible
> than function expressions.
Nor did I imply that you did.
> The second example is also "more verbose", it was intended to show how a
> common comparison function can be made reusable.
That was reasonable, given the context of the question that I could not see
(and did not want to consider here anyway).
> The difference in memory usage should be minimal. A declared function
> will remain in memory at least until it goes out of scope and can be
> garbage collected (when no references to it are left). With function
> expressions, no reference to the function is left after it has been used
> to sort the array. I don't expect a measureable difference either way.
I was recommending that if you need the same comparator more than once, you
should not do so by using the same function expression more than once in an
argument list. So not
var
a = [3, 1, 2],
b = [5, 6, 4],
c = [8, 9, 7];
a.sort(function (x, y) { return x - y; });
b.sort(function (x, y) { return x - y; });
c.sort(function (x, y) { return x - y; });
but instead
var
a = [3, 1, 2],
b = [5, 6, 4],
c = [8, 9, 7],
f = function (x, y) { return x - y; };
a.sort(f);
b.sort(f);
c.sort(f);
That an object is no longer referred to does not mean that it has been
garbage-collected, so WLOG one must assume that every evaluation of a
function expression creates a new object with the "old" one still reserving
memory.
There might be internal optimizations in some implementations that would not
create a new Function instance for the *same* function expression (but it
would not be trivial to implement this, because of the closure); but it is
unwise to rely on that.
BTW, who would be interested in a sort method that would be able to only
sort parts of an array?
> OT: You didn't mention performance, but I was curious and did a quick
> test anyway: in current versions of Firefox and Chrome, there are no
> huge differences, although referencing a previously created function is
> consistently faster than using an inline expression,
I expected that.
> and traditional functions are consistently faster than arrow functions.
I did not expect that …
> In Firefox 44, inline arrow functions are noticably slower than the other
> variants (about an order of magnitude).
… that – so, are we, in the end, sacrificing performance for convenient
syntax with arrow functions here? –,
> By the way, const/let are also consistently slower than var, but not by
> much. This may be caused by the additional checks required for the
> "temporal dead zone".
… or that. Thank you for sharing this.
--
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
Bubblesort jonas.thornvall@gmail.com - 2016-03-04 02:00 -0800
Re: Bubblesort "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-03-04 18:53 +0800
Re: Bubblesort "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-04 12:01 +0100
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 03:03 -0800
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 05:15 -0800
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 05:41 -0800
Re: Bubblesort "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-03-04 21:56 +0800
Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-04 15:10 +0100
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 07:37 -0800
Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-04 17:08 +0100
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 07:50 -0800
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 07:54 -0800
Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-04 17:21 +0100
Re: Bubblesort Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-03-04 19:26 +0000
Re: Bubblesort "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-04 23:05 +0100
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-05 09:15 -0800
Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-08 03:43 +0100
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-08 02:41 -0800
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-08 09:10 -0800
Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-08 23:34 +0100
Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-08 23:46 +0100
Re: Bubblesort Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-03-09 00:16 +0000
Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-09 02:41 +0100
Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-08 23:35 +0100
Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-09 02:38 +0100
Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-09 22:04 +0100
Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-10 21:14 +0100
Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-10 22:17 +0100
Re: Bubblesort "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-10 22:31 +0100
Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-10 23:25 +0100
Re: Bubblesort "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-11 00:16 +0100
Re: Bubblesort "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-03-04 21:59 +0800
Re: Bubblesort "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-04 17:12 +0100
Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-05 02:00 +0100
Re: Bubblesort John Harris <niam@jghnorth.org.uk.invalid> - 2016-03-05 18:39 +0000
Re: Bubblesort Scott Sauyet <scott.sauyet@gmail.com> - 2016-03-05 17:40 -0800
Re: Bubblesort "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-03-04 21:54 +0800
Re: Bubblesort "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-04 16:59 +0100
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 03:04 -0800
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 03:55 -0800
Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-05 00:01 +0100
Re: Bubblesort Aleksandro <aleksandro@gmx.com> - 2016-03-06 18:41 -0300
csiph-web