Created
November 2, 2010 19:13
-
-
Save gcoop/660128 to your computer and use it in GitHub Desktop.
A load of JavaScript things that can be done slowly or quickly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// == parseInt(); | |
var width = parseInt("12.5"); // Slow | |
var width = ~~(1 * "12.5"); // Fast 1 * "12.5" turns string to float, ~~ double bit operator floors float to int. Chrome this is slower. | |
// == new Array(); | |
var arr = new Array(); // Slow | |
var obj = new Object(); // Slow | |
var arr = []; // Fast | |
var obj = {}; // Fast | |
// == with | |
with (obj) { // Slow | |
console.log(p); | |
} | |
var i = 10; | |
while (i--) console.log(obj[i].p); // Fast | |
// == try/catch | |
try { // Very slow, last resort only. | |
console.log('I am slow'); | |
} catch(e) {} | |
/** | |
* Sources | |
* | |
* 1. http://net.tutsplus.com/tutorials/javascript-ajax/extreme-javascript-performance/ | |
* | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment