Skip to content

Instantly share code, notes, and snippets.

@gcoop
Created November 2, 2010 19:13
Show Gist options
  • Save gcoop/660128 to your computer and use it in GitHub Desktop.
Save gcoop/660128 to your computer and use it in GitHub Desktop.
A load of JavaScript things that can be done slowly or quickly.
// == 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