本文实例讲述了JavaScript对数组进行随机重排的方法。分享给大家供大家参考。具体如下:
var count = 100000,arr = [];
for(var i=0;i.5 ? -1 : 1;});
document.write(arr+'
'); var t1 = new Date().getTime(); document.write(t1-t); //以下方法效率最高 if (!Array.prototype.shuffle) { Array.prototype.shuffle = function() { for(var j,x,i = this.length; i; j = parseInt(Math.random() * i),x = this[--i],this[i] = this[j],this[j] = x); return this; }; } var t = new Date().getTime(); arr.shuffle(); document.write('
'+arr+'
'); var t1 = new Date().getTime(); document.write(t1-t);
'); var t1 = new Date().getTime(); document.write(t1-t); //以下方法效率最高 if (!Array.prototype.shuffle) { Array.prototype.shuffle = function() { for(var j,x,i = this.length; i; j = parseInt(Math.random() * i),x = this[--i],this[i] = this[j],this[j] = x); return this; }; } var t = new Date().getTime(); arr.shuffle(); document.write('
'+arr+'
'); var t1 = new Date().getTime(); document.write(t1-t);
希望本文所述对大家的javascript程序设计有所帮助。