Skip to content

Instantly share code, notes, and snippets.

@chetandhembre
Last active August 29, 2015 14:07
Show Gist options
  • Save chetandhembre/e402a05831b1eaf98860 to your computer and use it in GitHub Desktop.
Save chetandhembre/e402a05831b1eaf98860 to your computer and use it in GitHub Desktop.
best practicess while writing javascript
1. No anonymous function give function name even in callback..V8 optimized hot function if your function do not have name then each time it get calls V8 think it is new function and tend optimize it or may not consider as hot function
2. Declare your object (initialized it). Adding and removing attribute on the fly is not optimized way to do things. See how V8's hidden call concept. And always initialize variable in same sequence.
3. use prototype to bind function to your object.
4. use buffer more often. Converting to 'utf8' string is costly
5. use 'dezalgo' module, You can find more reason behind it in link to blog post from readme of module
6. do not string concatenate using '+' operator create array of all string an use join method.
7. do not create function inside closure .. write atomic function (named function )
8. assign static constant value to varible in class using prototype
9. Do not use delete reason hidden class optimization assign null or let GC to it's job
10. try to avoid number more than 31 bits.
11. try to have array with index starting from 0.
12. Do not delete element from array
13. DO not do this
var a = new Array()
for (var i = 0; i < b.length; i++ ) {
a[0] |= b[0]
}
Instead do this
var a = new Array()
a[0] = 0
for (var i = 0; i < b.length; i++ ) {
a[0] |= b[0]
}
14 . always have same type of element in array or if you can not then initialized array with different type in single one.
15. small array allocate correct size start.
16. Do not add object to numeric array
17. use monomorphic operation over polymorphic operation. Monomorphic operatin is where both operand are same like 1 + 2 but polymorphic operation is 1 + undefined.
18. avoid try catch block. If you really want to do it create another function which has try catch block and can be use by other code requires try catch.
19. do not change type (hidden class) of object on run time. it will de optimized your code.
20. read https://github.com/petkaantonov/bluebird/wiki/Optimization-killers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment