我需要在ES6中将字符串转换为TypedArrays(并返回到字符串)。 目前,这是通过以下功能完成的:
string2array
array2string
比string2array(s):
allocate memory for array of length=s.length
foreach character (or surrogate pair):
get codePoint
push to array
return array
array2string(a):
allocate memory for string of lenght=a.length
foreach item in array
get String.fromCodePoint
push to string
return string
慢两倍。我希望这两个功能同样快。我的工作方式如下:
function testConversions() {
"use strict";
const data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
const iterations = 1e1;
let a;
let s;
let i;
function string2array(s) {
return Uint32Array.from(s, (c) => c.codePointAt(0));
}
function array2string(a) {
return String.fromCodePoint(...a);
}
console.time("s2a");
i = iterations;
while (i) {
a = string2array(data);
i -= 1;
}
console.timeEnd("s2a");
console.log(a.toString());
console.time("a2s");
i = iterations;
while (i) {
s = array2string(a);
i -= 1;
}
console.timeEnd("a2s");
console.log(s);
}
testConversions();
所以他们看起来很像我。
这是我的测试用例:
"<uses-permission android:name="android.permission.MANAGE_NETWORK_POLICY"/>"
but it hints that :
**permission is only granted to system apps..... **
答案 0 :(得分:0)
您的测试应该与众不同,因为string2array
的效果超过array2string
。
如果您创建:
string_2_code_point_array
code_point_array_2_uint_array
uint_array_2_string
你会看到最后两个有相似的时间。