r/javascript • • Sep 06 '12

JavaScript Tricks You May Not Know

http://www.learncomputer.com/javascript-tricks-you-may-not-know/
0 Upvotes

14 comments sorted by

4

u/kenman Sep 06 '12

Ugh, another problematic "tricks" article.

  • Deeply Nested Objects

This is not a "trick", it's JS 101. RTFM.

  • Static Local Variables

This one can be useful, but it's misnamed as they're not local.

  • Assertion

A better title would have been "how to make your own exceptions", which is a fancy way of demonstrating that you can use basically any function as an exception.

  • Custom Comparison and Sorting

Again, RTFM.

  • Tying Methods to Objects

Or, "let me demonstrate again how I haven't RTFM". See also: call(), apply(), and the prototypal inheritance.

  • Concatenating Strings vs. Array.join

I believe this one is flat-out wrong.

2

u/kristopolous Sep 06 '12 edited Sep 06 '12

last one isn't wrong. Depends on the platform. in IE it's totally true ... although probably not in the upcoming ie10. If the article was dated say March 3, 2008, it would be universally true.

1

u/kenman Sep 06 '12

No, you're wrong.

Check out any of the existing jsperf's, or create your own.

I just ran the this test on IE8 and IE9, here's the results: IE8, IE9

That is, in IE8 it's 2195 vs 348 ops/sec (almost 7x's slower), and in IE9 it's 19929 vs. 2834 ops/sec (around 8 times slower).

2

u/kristopolous Sep 06 '12

ok. I'm being serious here, I'll get back to you after I catch this train. I know for a fact that push was faster than += historically; I was pretty vocal about making that no longer the case a few years back. I thought IE8 had the problem still, but I could be wrong; unless not all IE8s use the same JScript revision core.

2

u/kristopolous Sep 06 '12

ok so here's the first one, it refers to inline object forming: http://qaa.ath.cx/string-array-perf.html

In chrome the first method on my machine is 28ms, the second throws an error and doesn't run (I don't know whether this is ECMA spec or the Googs fault here).

In modern FF, the + scenario is 296 v. 423 ... + being faster, but not by much. My MBP doesn't have ie right now ... but yes, we've already established that I'm not claiming that array.push is faster now, in 2012 ... let me see what else I can dig up.

2

u/kenman Sep 06 '12

So, are we in agreement that + is faster than join()? You initially objected to my statement of such (with "in IE it's totally true" and then hinting that I may be correct in the future with IE10), but now you seem to be conceding the point.

(And in light of the article being "tricks" for modern web dev, I don't see much need to argue about what may have been the case in the past -- though I'm fully confident that any IE6/7 tests will show the same thing that I've been saying this whole time; perhaps you may find browser X at version Y will be faster with join(), but as far as blanket statements go, I think we can disqualify the OP's as false.)

2

u/kristopolous Sep 06 '12

I agree with that. I just vividly recall this history that I purport. I'm just worried that this past isn't how I remember it.

1

u/kristopolous Sep 06 '12

ok, I don't have the browsers at my disposal to do the other tests right now ... but here's the drama about this issue: https://bugzilla.mozilla.org/show_bug.cgi?id=56940

1

u/kenman Sep 06 '12

It looks to me like that is a discussion of using + vs. +=, no?

I see one comment there referencing join(), but that author didn't justify his claims in any way and I don't think anyone else even responded to it.

On top of that, that bug was created in 2000; as far as web dev goes, that's eons ago and IMO 100% irrelevant when discussion performance issues of code in the year 2012.

3

u/bart2019 Sep 06 '12

The dude should try to avoid words collapsed together, like

varmyVariable;
deletesomeObj.val;

which should have been

var myVariable;
delete someObj.val;

3

u/kristopolous Sep 06 '12 edited Sep 06 '12

agreed. It's totally sloppy. And these are all things that someone with a thorough JS education should know.

Here's an example of something this could have been, in firefox, try this:

function debugger() { alert ("hello world"); }
debugger();

Or the fact that the javascript duck typing can be overloaded. That's something most people actually don't know.

-1

u/samplebitch Sep 06 '12

duck typing?

0

u/[deleted] Sep 06 '12

[deleted]

1

u/mgoulart Sep 06 '12

Don't be that guy. Link him directly to it: http://lmgtfy.com/?q=duck+typing

-1

u/samplebitch Sep 06 '12

Yeah I was too lazy to google. :)