document.getElement('id');
var slice = [].slice, split = "".split;
Reduce the Number of DOM Elements. A complex page means more bytes to download and it also means slower DOM access in JavaScript. It makes a difference if you loop through 500 or 5000 DOM elements on the page when you want to add an event handler for example.javascript
document.getElementsByTagName('*').length
獲取頁面中dom元素的數量。var ul = document.getElementById('id'), fragment = document.createDocumentFragment(), data = ['text1','text2','text3'], li; for(var i = 0,len = data.length; i < len; i++) { li = document.createElment('li'); li.appendChild(document.createTextNode(data[i])); fragment.appendChild(li); } ul.appendChild(fragment);
Joining strings using the plus sign (ie var ab = 'a' + 'b';) creates performance issues in IE when used within an iteration. This is because, like Java and C#, JavaScript uses unmutable strings. Basically, when you concatenate two strings, a third string is constructed for gathering the results with its own object instantiation logic and memory allocation. While other browsers have various compilation tricks around this, IE is particularly bad at it.html
var Person = Object.create({ init: function(name) { this.name = name; }, do: function(callback) { callback.apply(this); } }); var john = new Person('john'); john.do(function() { alert(this.name); // 'john' gets alerted because we rewired 'this'. });