Created
September 24, 2010 19:15
-
-
Save bga/595870 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($G) | |
{ | |
var _lengthGetter = function() | |
{ | |
var b = -1; | |
var i; | |
for( ;; ) | |
{ | |
i = 1; | |
while(this.hasOwnProperty(b + i)) | |
i <<= 1; | |
if(i == 1) | |
break; | |
b += i >>= 1; | |
// 3 steps back | |
if(i && this.hasOwnProperty(b + (i >>= 1))) | |
b += i; | |
if(i && this.hasOwnProperty(b + (i >>= 1))) | |
b += i; | |
if(i && this.hasOwnProperty(b + (i >>= 1))) | |
b += i; | |
} | |
return b + 1; | |
} | |
var _lengthSetter = function(nLen) | |
{ | |
var cLen = +this.length; | |
if(cLen < nLen) | |
{ | |
var i = cLen - 1, u = undefined; while(++i < nLen) | |
this[i] = undefined; | |
} | |
else | |
{ | |
var i = +nLen; while(--i >= cLen) | |
delete this[i]; | |
} | |
}; | |
var FastSolidArray; | |
if(Object.defineProperty) | |
{ | |
var lengthProp = | |
{ | |
get: _lengthGetter, | |
set: _lengthSetter, | |
configurable: false, | |
enumerable: false | |
}; | |
FastSolidArray = function(len) | |
{ | |
Object.defineProperty( | |
this, | |
'length', | |
lengthProp | |
); | |
if(len != null) | |
this.length = len; | |
}; | |
} | |
else if({}.__defineGetter__ && {}.__defineSetter__) | |
{ | |
FastSolidArray = function(len) | |
{ | |
this.__defineGetter__('length', _lengthGetter); | |
this.__defineSetter__('length', _lengthSetter); | |
if(len != null) | |
this.length = len; | |
}; | |
} | |
FastSolidArray.prototype = new Array(); | |
FastSolidArray.prototype.constructor = FastSolidArray; | |
$G.FastSolidArray = FastSolidArray; | |
})(this); | |
// test | |
(function($G) | |
{ | |
var _fill = function(a, c) | |
{ | |
var i = a.length >>> 0; | |
while(i--) | |
a[i] = c; | |
}; | |
var m = 1000; | |
_speedTest( | |
[ | |
function(n) | |
{ | |
var a = new Array(m); | |
var i = n >>> 0; while(i--) | |
_fill(a, 1); | |
}, | |
function(n) | |
{ | |
var a = new FastSolidArray(m); | |
var i = n >>> 0; while(i--) | |
_fill(a, 1); | |
} | |
], | |
1000 | |
); | |
})(this); | |
/* | |
opera 10.60 | |
m = 1000000 | |
0: 5484 ms | |
1: 5080 ms | |
m = 100000 | |
0: 4869 ms | |
1: 3225 ms | |
m = 10000 | |
0: 3009 ms | |
1: 2595 ms | |
m = 1000 | |
0: 3053 ms | |
1: 3143 ms | |
m = 100 | |
0: 4981 ms | |
1: 7834 ms | |
*/ (function($G) | |
{ | |
var _lengthGetter = function() | |
{ | |
var b = -1; | |
var i; | |
for( ;; ) | |
{ | |
i = 1; | |
while(this.hasOwnProperty(b + i)) | |
i <<= 1; | |
if(i == 1) | |
break; | |
b += i >>= 1; | |
// 3 steps back | |
if(i && this.hasOwnProperty(b + (i >>= 1))) | |
b += i; | |
if(i && this.hasOwnProperty(b + (i >>= 1))) | |
b += i; | |
if(i && this.hasOwnProperty(b + (i >>= 1))) | |
b += i; | |
} | |
return b + 1; | |
} | |
var _lengthSetter = function(nLen) | |
{ | |
var cLen = +this.length; | |
if(cLen < nLen) | |
{ | |
var i = cLen - 1, u = undefined; while(++i < nLen) | |
this[i] = undefined; | |
} | |
else | |
{ | |
var i = +nLen; while(--i >= cLen) | |
delete this[i]; | |
} | |
}; | |
var FastSolidArray; | |
if(Object.defineProperty) | |
{ | |
var lengthProp = | |
{ | |
get: _lengthGetter, | |
set: _lengthSetter, | |
configurable: false, | |
enumerable: false | |
}; | |
FastSolidArray = function(len) | |
{ | |
Object.defineProperty( | |
this, | |
'length', | |
lengthProp | |
); | |
if(len != null) | |
this.length = len; | |
}; | |
} | |
else if({}.__defineGetter__ && {}.__defineSetter__) | |
{ | |
FastSolidArray = function(len) | |
{ | |
this.__defineGetter__('length', _lengthGetter); | |
this.__defineSetter__('length', _lengthSetter); | |
if(len != null) | |
this.length = len; | |
}; | |
} | |
FastSolidArray.prototype = new Array(); | |
FastSolidArray.prototype.constructor = FastSolidArray; | |
$G.FastSolidArray = FastSolidArray; | |
})(this); | |
// test | |
(function($G) | |
{ | |
var _fill = function(a, c) | |
{ | |
var i = a.length >>> 0; | |
while(i--) | |
a[i] = c; | |
}; | |
var m = 1000; | |
_speedTest( | |
[ | |
function(n) | |
{ | |
var a = new Array(m); | |
var i = n >>> 0; while(i--) | |
_fill(a, 1); | |
}, | |
function(n) | |
{ | |
var a = new FastSolidArray(m); | |
var i = n >>> 0; while(i--) | |
_fill(a, 1); | |
} | |
], | |
1000 | |
); | |
})(this); | |
/* | |
opera 10.60 | |
m = 1000000 | |
0: 5484 ms | |
1: 5080 ms | |
m = 100000 | |
0: 4869 ms | |
1: 3225 ms | |
m = 10000 | |
0: 3009 ms | |
1: 2595 ms | |
m = 1000 | |
0: 3053 ms | |
1: 3143 ms | |
m = 100 | |
0: 4981 ms | |
1: 7834 ms | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment