快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

葡京娱乐场968119_龟发之家论坛



Javascript中的数组是个强大年夜的家伙:

你可以创建的时刻不规定长度,而是动态的去改变长度。

你可以把他当成通俗的数组去读取,也可以当他是客栈来应用。

你可以改变数组中每个元素的值以致是类型。

好吧,着实他是一个工具,比如我们可以这样去创建数组:

var array = new Array(10);

构造函数

上面我们经由过程ArrayBuffer来创建TypedArray,而实际上,TypedArray供给了3个构造函数来创建他的实例。

构造函数

TypedArray(unsigned long length)创建一个新的TypedArray,length是其固定长度。

TypedArray(TypedArray array)TypedArray(type[] array)创建一个新的TypedArray,其每个元素根据array进行初始化,元素进行了响应的类型转换。

TypedArray(ArrayBuffer buffer, optional unsigned long byteOffset, optional unsigned long length)创建一个新的TypedArray,使其作为buffer的一个引用,byteOffset为其肇真个偏移量,length为其长度。

以是平日我们用下面的要领创建TypedArray:

var array = new Uint8Array(10);

数组类型

类型

大年夜小

描述

Web IDL类型

C 类型

Int8Array

1

8位有符号整数

byte

signed char

Uint8Array

1

8位无符号整数

octet

unsigned char

Uint8ClampedArray

1

8位无符号整数 (clamped)

octet

unsigned char

Int16Array

2

16位有符号整数

short

short

Uint16Array

2

16位无符号整数

unsigned short

unsigned short

Int32Array

4

32位有符号整数

long

int

Uint32Array

4

32位无符号整数

unsigned long

unsigned int

Float32Array

4

32位IEEE浮点数

unrestricted float

float

Float64Array

8

64位IEEE浮点数

unrestricted double

double

玩过canvas的可能会感觉很眼熟。

由于ImageData顶用于存储图像数据的数组就是Uint8ClampedArray类型的。

例如:

var context = document.createElement("canvas").getContext("2d");

var imageData = context.createImageData(100, 100);

console.log(imageData.data);

Test1:顺序读取速读

var timeArray1 = [];

var timeArray2 = [];

function check1(){var array = new Uint8ClampedArray(5000000);

for(var i = array.length; i--;){array[i] = Math.floor(Math.random() * 100);

}var temp;

var time1 = (new Date()).getTime();for(var i = array.length; i--;){

temp = array[i];}

var time2 = (new Date()).getTime();console.log(time2 - time1);

timeArray1.push(time2 - time1); }

function check2(){

var array2 = new Array(5000000);for(var i = array2.length; i--;){

array2[i] = Math.floor(Math.random() * 100);}

var temp;var time3 = (new Date()).getTime();

for(var i = array2.length; i--;){temp = array2[i];

}var time4 = (new Date()).getTime();

console.log(time4 - time3);timeArray2.push(time4 - time3);

}

function timer(__fun, __time, 葡京娱乐场968119__callback){var now = 0;

function begin(){var timeout = setTimeout(function(){

if(now !== __time){now++;

__fun();begin();

}else{if(timeArray1.length && timeArray2.length){

console.log("timeArray1 == " + timeArray1 + ", average == " + average(timeArray1));console.log("timeArray2 == " + timeArray2 + ", average == " + average(timeArray2));

}__callback && __callback();

}}, 100);

}begin();

}

function average(__array){var total = 0;

for(var i = __array.length; i--;){total += __array[i];

}return (total / __array.length);

}

timer(check1, 10, function(){timer(chec葡京娱乐场968119k2, 10);

});

可见Uint8ClampedArray的读取速率显着比Array要快(条状柱越长,代表花费光阴越多)。

Test2:随机读取

//&hellip葡京娱乐场968119;…

function check1(){

var array = new Uint8ClampedArray(5000000);for(var i = array.length; i--;){

array[i] = Math.floor(Math.random() * 100);}

var temp;var time1 = (new Date()).getTime();

for(var i = array.length; i--;){temp = array[Math.floor(Math.random() * 5000000)];

}var time2 = (new Date()).getTime();

console.log(time2 - time1);timeArray1.push(time2 - time1);

}

function check2(){var array2 = new Array(5000000);

for(var i = array2.length; i--;){array2[i] = Math.floor(Math.random() * 100);

}var temp;

var time3 = (new Date()).getTime();for(var i = array2.length; i--;){

temp = array2[Math.floor(Math.random() * 5000000)];}

var time4 = (new Date()).getTime();console.log(time4 - time3);

timeArray2.push(time4 - time3); }

//……

随即读取中Uint8ClampedArray的读取速率也是比Array要快的。

Test3:顺序写入

//……

function check1(){

var array = new Uint8ClampedArray(5000000);var time1 = (new Date()).getTime();

for(var i = array.length; i--;){array[i] = Math.floor(Math.random() * 100);

}var time2 = (new Date()).getTime();

console.log(time2 - time1);timeArray1.push(time2 - time1);

}

function check2(){var array2 = new Array(5000000);

var time3 = (new Date()).getTime();for(var i = array2.length; i--;){

array2[i] = Math.floor(Math.random() * 100);}

var time4 = (new Date()).getTime();console.log(time4 - time3);

timeArray2.push(time4 - time3); }

//……

Test4:复制操作(U8C to U8C 和 Array to U8C)

//……

function check1(){

var array = new Uint8ClampedArray(5000000);for(var i = array.length; i--;){

array[i] = Math.floor(Math.random() 葡京娱乐场968119* 100);}

var temp;var array2 = new Uint8ClampedArray(5000000);

var time1 = (new Date()).getTime();array2.set(array);

var time2 = (new Date()).getTime();console.log(time2 - time1);

timeArray2.push(time2 - tim葡京娱乐场968119e1); }

function check2(){

var array = new Array(5000000);for(var i = array.length; i--;){

array[i] = Math.floor(Math.random() * 100);}

var temp;var array2 = new Uint8ClampedArray(5000000);

var time1 = (new Date()).getTime();array2.set(array);

var time2 = (new Date()).getTime();console.log(time2 - time1);

timeArray2.push(time2 - time1); }

//……

可见U8C复制到U8C,比Array复制到U8C快得多。

参考文献

Typed Array Specification

您可能还会对下面的文章感兴趣: