Not only bug-fixes, but free performance increases...
Find in 1.4.2: 74,057 ops/sec
Find in 1.8.2: 127,227 ops/sec
Find in 1.9.0: 222,108 ops/sec
<head>
<script>
$(document).ready(function() {
doStuff();
});
</script>
</head>
<body>
...
<script>
doStuff();
</script>
</body>
$('.button').css('opacity', 0.5);
$('.button').css('padding', '20px');
$('.button').addClass('selected');
$('.button')
.css('opacity', 0.5)
.css('padding', '20px')
.addClass('selected');
//OR, store it in a variable:
var $button = $('.button');
No Caching: 20,479 ops/sec
Caching: 24,518 ops/sec
var $inputs = $('div:input');
var $inputs = $('div').filter(':input');
Q: How can hell can I keep track of all these jQuery selector performance tips?
A: Don't! Just check the jQuery documentation, jsperf tests to see if your selector might be improved
Because :input is a jQuery extension and not part of the CSS specification, queries using :input cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. To achieve the best performance when using :input to select elements, first select the elements using a pure CSS selector, then use .filter(":input").
for(var i = 0, len = arr.length; i < len; i++) {
arr[i].on('click', function(event) { /*...*/ });
}
var clickListener = function(event) { /*...*/ };
for(var i = 0, len = arr.length; i < len; i++) {
arr[i].on('click', clickListener);
}
Protip: JS linter will point this mistake out to you.
for(var i in arr) {
console.log(arr[i]);
}
for(var i = 0, len = arr.length; i < len; i++) {
console.log(arr[i]);
}
For..in loop: 4,076 ops/sec
Cached For loop: 326,242 ops/sec
I usually don't have to loop through a million things at a time!
(Besides, that for in loop is much easier to read!)
"use strict";
eval()
with
(too confusing)
var nine = parseInt('09'); // Could be zero!!!
var nine = parseInt('09', 10);
...Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.
function swap(array, a, b) {
temp = array[b];
array[b] = array[a];
array[a] = temp;
}
function swap(array, a, b) {
var temp = array[b];
array[b] = array[a];
array[a] = temp;
}
var a, b;
var a = b = 2;
console.log(window.a); //undefined
console.log(window.b); //2, oh no!
var a, b;
a = b = 2;
console.log(window.a); //undefined
console.log(window.b); //undefined
greeting = 'hello'; //evil global variable
function f() {
console.log(greeting);
var greeting = 'goodbye';
console.log(greeting);
}
> undefined
> goodbye
greeting = 'hello'; //evil global variable
function f() {
var greeting;
console.log(greeting); //undefined (NOT 'hello'!)
greeting = 'goodbye';
console.log(greeting); //sup yo
}
Solution: Define your variables in the top of your file/function to avoid surprises!
return
{
hotDogs: 'tasty'
};
return {
hotDogs: 'tasty'
};
undefined
if(n == undefined) { /*...*/ }
if(typeof n === "undefined") { /*...*/ }
undefined
var n;
// OK - prints true:
console.log(n == undefined);
// OK - prints true:
console.log(typeof aNonExistantVariable == 'undefined');
// BAD - this throws an errror:
console.log(aNonExistantVariable == undefined);
Well, you shouldn't have a non-existant variable in the first place!
undefined
function bad() {
var undefined = "Problem?"; // Still works in strict mode!
// ...
if(n == undefined) {
// This code will only run if n equals "Problem?",
// and it probably doesn't.
}
}
Protip: Don't be that guy that makes an undefined
variable.
var n = '';
// ...
if(n == 0) {
// This code will be ran because '' == 0 is true
}
var n = '';
// ...
if(n === 0) {
// This code won't run because '' === 0 is NOT true
}
Expression | Result | ||
---|---|---|---|
'' | == | "0" | false |
'' | == | 0 | true |
0 | == | "0" | true |
false | == | "false" | false |
false | == | "0" | true |
false | == | undefined | false |
false | == | null | false |
null | == | undefined | true |
"\t" | == | 0 | true |