Doge log

Abby CTO 雑賀 力王のオフィシャルサイトです

DOMで取得する速度を上げる その2

前回は基本的な考え方です。
実際にはこうなります。

nodeCache : {},

elementsById : function(id){
	if(!(id instanceof String) && typeof id != "string"){
		return [id];
        }  		
	var nodes = [];
	var elem = $i(id);
	if(!this.nodeCache[id]){
		this.nodeCache[id] = []; 
	}
	while(elem){
		if(!elem["cached"]){
			nodes.push(elem);
		}
		elem.id = "";
		elem = $i(id);
	}
	this.nodeCache[id] = this.nodeCache[id].concat(nodes);
	this.nodeCache[id].map(function(n){
		n.id = id;
		n.cached = true;
	} );
	return this.nodeCache[id];
},

うむ。まんまだね。
ホントは

return {'add':nodes, 'all' : this.nodeCache[id]}
  • 今回対象になったidの分
  • 全てのid

を選べた方がいいのかな。
load時に以外に再度id振りなおした場合とかで使える。
全部のidに再度イベントを登録し直す羽目になると元々load時に積んだイベントが無駄になるしリークの元になる。
なのでそこはまた別途対応ですよ。

うくく。