Large

YouTube HD Ultimate

By Avindra V.G. Last update Dec 10, 2010 — Installed 402,019 times.

Optimizations

in
Subscribe to Optimizations 5 posts, 2 voices



JoeSimmons Scriptwright

This thread is only for optimizations

  • Switched from regex to indexOf since it's 4x faster. if(/^on/.test(b)) to if(b.indexOf('on')==0)

 
JoeSimmons Scriptwright

You can shorten and simply the code by creating arrays like var array = [1, 2, 3,]; instead of var array = new Array(1, 2, 3); so it's easier to read.



You do this: this.textContent="Hide Ultimate Options"; but sometimes the value of this changes as stated by mozilla. You should use E.currentTarget instead of this.

 
Avindra V.G. Script's Author

I switched from literal array notation because I tested it and it was actually slower.

var fns = new Array (
	function() {
		return [1, 2, 3, 4];
	},
	function() {
		return new Array(1, 2, 3, 4);
	}
), res=new Array();

for(var f = 0; f < fns.length; f++) {
	var st = new Date();
	for(var i=10000; i>=0; --i)
		fns[f]();
	res.push("#" + f + ":\t" + (new Date() - st));
}
res.join("\n");

Unless I did something wrong. I can't imagine why this is the case.

 
JoeSimmons Scriptwright

Hmm you're right... it's 1.2 times faster with new Array(). Now I'm wondering why that is.

 
JoeSimmons Scriptwright

Optimized create() code a bit...

// Create by avg, modified by JoeSimmons
function create(a,b) {
	var ret=document.createElement(a);
	if(b) for(var prop in b) {
		if(prop.indexOf('on')==0) ret.addEventListener(prop.substring(2),b[prop],false);
		else if(prop=="kids" && (prop=b[prop])) {
			for(var i=0;i<prop.length;i++) ret.appendChild(prop[i]);
		}
		else if('style,accesskey,id,name,src,href,class'.indexOf(prop)!=-1) ret.setAttribute(prop, b[prop]);
		else ret[prop]=b[prop];
	}  return ret;
}

Cross
Presentational HTML allowed.
Use <code> for inline code and <pre> for code blocks. Use &lt; and &gt; for literal < and >.
We help break paragraphs and link your links.
or cancel