BonsaiDen / JavaScript-Garden

A collection of documentation about the most quirky parts of the JavaScript language.
http://bonsaiden.github.com/JavaScript-Garden
MIT License
3.45k stars 558 forks source link

Some enhancements #19

Closed evilpie closed 13 years ago

evilpie commented 13 years ago

Just noticed that this is hosted on github, after sending an email, dooh. Here is the copy:

The arguments Object "The arguments is, except for the two cases named at the start of this section, always created." Not really true, if it's not used in any function having access to this scope it's not created in the most cases and engines. "Array.prototype.slice.call" Please don't show this code, somebody will copy it, make some benchmarks. Array.apply(null, arguments); should be at least twice as fast usually, but still this should avoided as often as possible.

The class of an object Just wanted to note that a ES5 errata changed Object.prototype.toString, so toString.call(null) returns [object Null] and undefined [object Undefined] In general, there is no need use the strict equality operator with typeof, but that's more an personal preference.

undefined and null "But this variable is not a constant, meaning that it can be easily overwritten which then leads to abstruse bugs" undefined is not writable in ES 5, but still not an keyword (so it could be shadowed) "The value undefined is returned in the following cases:" The list is not conclusive (eg void 0, [].shift()), i would change to "some example to get the undefined value"

BonsaiDen commented 13 years ago

The arguments Object

The spec never says that it can be omitted, also, I did some testing in recent browsers there was no speed difference at all between functions that used arguments and function that don't.

The Array.prototype.slice.call is there to show how to convert it to an array, foo.apply(some, arguments) can't be used all the time, of course it would be good to note there just in case people still don't get it after reading the rest of the guide.

"but still this should avoided as often as possible" there is no performance penalty whatsoever.

ES5 errata

Good catch, I'll add that as a ES5 note, IE in quirks still returns object for both undefined and null though.

undefined

Yes, should be changed to "some examples"

.

BonsaiDen commented 13 years ago

The last 4 commits contain these changes, please have a look:
https://github.com/BonsaiDen/JavaScript-Garden/commits/master

evilpie commented 13 years ago

arguments Object At least SpiderMonkey loses some optimizations when arguments is involved and is used in weird ways. foo.apply(some, arguments) should work all the time, i don't see a problem there.

"but still this should avoided as often as possible" there is no performance penalty     whatsoever.

Just wanted to say that converting the arguments object isn't something you should do just for convenience.

Type Casting Just noticed that you introduced octal numbers there, these are forbidden in strict mode and are generally discouraged, because they look like normal numbers (base 10)

undefined Note what I said about undefined being changeable.

evilpie commented 13 years ago

Oh missed this while typing.

Notes on commit: undefined and null don't have a [[Class]], because they are not objects, toString just returns these new values for convenience. I don't see how the new example to get undefined helps. But you should still say that the global property undefined isn't changeable anymore (javascript has not notion of constants yet, so i would avoid using this word).

BonsaiDen commented 13 years ago

arguments Object Then TraceMonkey must have fixed that, did the tests in latest Chrome, Opera and FX4 Beta.

Add some further notes:
https://github.com/BonsaiDen/JavaScript-Garden/commit/6d2b8aa2ed70689193b6a10d1b2e9501782b3973

evilpie commented 13 years ago

I guess you didn't trigger anything that gets slow, could i see your test for arguments undefined is not writable in non-strict mode either. Trivia: Before the ES5 undefined/null as this got converted to the global object, that's why you got "object".

Btw, Ein Gruß aus Hessen :)

BonsaiDen commented 13 years ago

Hm, maybe, let me see if I can find them over at jsperf.There were some slight difference in mirco benching, but after I threw in some Djikstra those were all gone.

Hm? Only happens in strict mode for me, I hardly doubt that's implemented anywhere in this way (FX4 test says: only in strict mode) as it would break backwards compatibility.

Yeah, makes sense. Sorry if I miss stuff here and there, I'm extremely nervous at the moment due to an ongoing job application process (for a really awesome job).

PS: Gruß zurück :)

BonsaiDen commented 13 years ago

Since all of this seems to be fixed now, I'm closing. In case of further changes just fork and send some pull requests :)

jdalton commented 13 years ago

http://jsperf.com/arguments-speed

screenshot

BonsaiDen commented 13 years ago

Less micro benching please, the latter one might be removed completely depending on optimization levels:
http://jsperf.com/arguments-slowdown

jdalton commented 13 years ago

@DonsaiDen Plz check http://jsperf.com/arguments-slowdown/2

screenshot

As I said some engines may cut optimizations, the results are a little more than micro at this point, and certainly warrant some kind of note in the document.