Fugly JavaScript  Sometimes it’s good to be bad
Profiling CSI: Front End
Homespun Profiling
https://gist.github.com/1409341
The GoodHow to make friends and influence people
AlgorithmsSorts             IterationSearches          RecursionFilters           Functional ProgrammingCaching           ...
Unrolled Loops
Unrolling Epic Loops
Lookup Tables
Memoization
Expressions
The EvilHow to lose friends and alienate people
Falsey and Truthy        Falsey                 Truthy"" = false               "something" = true  0 = false              ...
Comparisons==, !=, ===, !==, >, >=, <, <=
Ternarycondition ? ifTrue : ifFalse
Bitwise&, |, ^, ~, <<, >>, >>>
Know thine enemy
Number[ digits ][ .digits ][ ( E | e ) [ ( + | - ) ] digits ]  x=0           x = -077  x = 123       x = NaN  x = -14     ...
128   64   32   16   8   4   2   11 0 1 1 0 1 0 1 = 181 -    64   32   16   8   4   2   11 0 1 1 0 1 0 1 = -53
Invisible Functions    ToInteger    ToInt32    ToUint32    ToUint16
var x = parseInt(x, 10);
Commaexpression1, expression2, expression 3;
Comma
Logical ORexpression1 || expression2;
Logical OR
Logical ANDexpression1 && expression2;
Logical AND
Fugly JavaScript
Fugly JavaScript
Fugly JavaScript
Fugly JavaScript
Fugly JavaScript
Fugly JavaScript
Fugly JavaScript
Fugly JavaScript
Fugly JavaScript
Fugly JavaScript
Fugly JavaScript
Fugly JavaScript
Upcoming SlideShare
Loading in …5
×

Fugly JavaScript

639 views
564 views

Published on

Using fugly JavaScript for optimisation.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
639
On SlideShare
0
From Embeds
0
Number of Embeds
24
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Some techniques break JSLint rulesOthers look bizarre or daftFine when you’re coding by yourselfRequire good reasons and good explanatory comments in a team environment
  • Plenty of tools availableSome even in the browsers
  • Doing it wrongTime from beginning to endDoing it rightCount number of iterations
  • There is an overhead to looping
  • Variation on Duff’s deviceIf we know we have a number of iterations that is a 32 bit number, we can dump the parseInt too
  • Dynamically typedDifference between equal and strict equal
  • Equal only compares valuesStrict equal compares values AND type
  • Returns one of two values based on the logical value of the condition
  • Perform functions on values as binary numbersTreat operands as a set of 32 bitsReturn standard numerical values
  • Technically a 64-bit number; actually 32-bit when doing anything bitwise.
  • Invisible functions
  • Converts to an integerConverts to a signed 32-bit integerConverts to an unsigned 32-bit integerConverts to an unsigned 16-bit integer
  • Allows multiple expressions to be evaluated in a single statementReturns the result of the last expression
  • Returns expr1 if it can be converted to true; otherwise, returns expr2.
  • Returns expr1 if it can be converted to false; otherwise, returns expr2.
  • Fugly JavaScript

    1. 1. Fugly JavaScript Sometimes it’s good to be bad
    2. 2. Profiling CSI: Front End
    3. 3. Homespun Profiling
    4. 4. https://gist.github.com/1409341
    5. 5. The GoodHow to make friends and influence people
    6. 6. AlgorithmsSorts IterationSearches RecursionFilters Functional ProgrammingCaching Object orientation
    7. 7. Unrolled Loops
    8. 8. Unrolling Epic Loops
    9. 9. Lookup Tables
    10. 10. Memoization
    11. 11. Expressions
    12. 12. The EvilHow to lose friends and alienate people
    13. 13. Falsey and Truthy Falsey Truthy"" = false "something" = true 0 = false "0" = true undefined = false 1 = true null = false -1 = true
    14. 14. Comparisons==, !=, ===, !==, >, >=, <, <=
    15. 15. Ternarycondition ? ifTrue : ifFalse
    16. 16. Bitwise&, |, ^, ~, <<, >>, >>>
    17. 17. Know thine enemy
    18. 18. Number[ digits ][ .digits ][ ( E | e ) [ ( + | - ) ] digits ] x=0 x = -077 x = 123 x = NaN x = -14 x = Number.MAX_VALUE = 1.7976931348623157e+308 x = 0.1541 x = Number.MIN_VALUE = 5e-324 x = NaN x = Number.NEGATIVE_INFINITY x = 0x1123 x = Number.POSITIVE_INFINITY x = -0xF1A7 x = Number.NaN x = 015
    19. 19. 128 64 32 16 8 4 2 11 0 1 1 0 1 0 1 = 181 - 64 32 16 8 4 2 11 0 1 1 0 1 0 1 = -53
    20. 20. Invisible Functions ToInteger ToInt32 ToUint32 ToUint16
    21. 21. var x = parseInt(x, 10);
    22. 22. Commaexpression1, expression2, expression 3;
    23. 23. Comma
    24. 24. Logical ORexpression1 || expression2;
    25. 25. Logical OR
    26. 26. Logical ANDexpression1 && expression2;
    27. 27. Logical AND

    ×