-
-
Save lahmatiy/90fa91d5ea89465d08c7db1e591e91c2 to your computer and use it in GitHub Desktop.
CSSO speed up talk – Object vs Array
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
1 create 85 | |
1 read 134 | |
1 create 90 | |
1 read 151 | |
--- | |
2 create 94 | |
2 read 500 | |
2 create 87 | |
2 read 437 | |
--- | |
3 create 105 | |
3 read 135 | |
3 create 107 | |
3 read 133 | |
--- | |
4 create 85 | |
4 read 134 | |
4 create 86 | |
4 read 130 | |
--- |
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
const TAB = 9; | |
const N = 10; | |
const F = 12; | |
const R = 13; | |
const SPACE = 32; | |
const DOUBLE_QUOTE = 34; | |
const QUOTE = 39; | |
const STAR = 42; | |
const SLASH = 47; | |
const ZERO = 48; | |
const NINE = 57; | |
var PUNCTUATION = { | |
9: 'Tab', // '\t' | |
10: 'Newline', // '\n' | |
13: 'Newline', // '\r' | |
32: 'Space', // ' ' | |
33: 'ExclamationMark', // '!' | |
34: 'QuotationMark', // '"' | |
35: 'NumberSign', // '#' | |
36: 'DollarSign', // '$' | |
37: 'PercentSign', // '%' | |
38: 'Ampersand', // '&' | |
39: 'Apostrophe', // '\'' | |
40: 'LeftParenthesis', // '(' | |
41: 'RightParenthesis', // ')' | |
42: 'Asterisk', // '*' | |
43: 'PlusSign', // '+' | |
44: 'Comma', // ',' | |
45: 'HyphenMinus', // '-' | |
46: 'FullStop', // '.' | |
47: 'Solidus', // '/' | |
58: 'Colon', // ':' | |
59: 'Semicolon', // ';' | |
60: 'LessThanSign', // '<' | |
61: 'EqualsSign', // '=' | |
62: 'GreaterThanSign', // '>' | |
63: 'QuestionMark', // '?' | |
64: 'CommercialAt', // '@' | |
91: 'LeftSquareBracket', // '[' | |
93: 'RightSquareBracket', // ']' | |
94: 'CircumflexAccent', // '^' | |
95: 'LowLine', // '_' | |
123: 'LeftCurlyBracket', // '{' | |
124: 'VerticalLine', // '|' | |
125: 'RightCurlyBracket', // '}' | |
126: 'Tilde' // '~' | |
}; | |
var CATEGORY_LENGTH = Math.max.apply(null, Object.keys(PUNCTUATION)) + 1; | |
var CATEGORY = new Array(CATEGORY_LENGTH); | |
for (var i = 0; i <= CATEGORY_LENGTH; i++) { | |
CATEGORY[i] = 0; | |
} | |
// fill categories | |
Object.keys(PUNCTUATION).forEach(function(key) { | |
CATEGORY[Number(key)] = 6; | |
}, CATEGORY); | |
var NUMBER = 2; | |
for (var i = 48; i <= 57; i++) { | |
CATEGORY[i] = NUMBER; | |
} | |
var WHITESPACE = 4; | |
CATEGORY[SPACE] = WHITESPACE; | |
CATEGORY[TAB] = WHITESPACE; | |
CATEGORY[N] = WHITESPACE; | |
CATEGORY[R] = WHITESPACE; | |
CATEGORY[F] = WHITESPACE; | |
var STRING = 3; | |
CATEGORY[QUOTE] = STRING; | |
CATEGORY[DOUBLE_QUOTE] = STRING; | |
function test(str) { | |
var result = []; | |
for (var i = 0; i < str.length; i++) { | |
var code = str.charCodeAt(i); | |
if (code === SLASH) { | |
var codeNext = i + 1 < str.length ? str.charCodeAt(i + 1) : 0; | |
if (codeNext === STAR) { | |
result.push({ type: 1, value: code, line: i + 1, column: 1, offset: 1 }); | |
i++; | |
continue; | |
} | |
} | |
if (code >= ZERO && code <= NINE) { | |
result.push({ type: 2, value: code, line: i + 2, column: 1, offset: 1 }); | |
} else if (code === DOUBLE_QUOTE || code === QUOTE) { | |
result.push({ type: 3, value: code, line: i + 3, column: 1, offset: 1 }); | |
} else if (code === SPACE || code === N || code === R || code === TAB || code === F) { | |
result.push({ type: 4, value: code, line: i + 4, column: 1, offset: 1 }); | |
} else if (code in PUNCTUATION) { | |
result.push({ type: 6, value: PUNCTUATION[code], line: i + 6, column: 1, offset: 1 }); | |
} else { | |
result.push({ type: 5, value: code, line: i + 5, column: 1, offset: 1 }); | |
} | |
} | |
return result; | |
} | |
function testToken(token) { | |
switch (token.type) { | |
case 1: | |
return token.column; | |
case 2: | |
return token.line; | |
default: | |
if (token.type === 3) { | |
return token.line; | |
} | |
} | |
return token.offset; | |
} | |
function testSum(tokens, fn) { | |
var res = 0; | |
for (var i = 0; i < tokens.length; i++) { | |
res += fn(tokens[i]); | |
} | |
return res; | |
} | |
function test2(str) { | |
var result = []; | |
for (var i = 0; i < str.length; i++) { | |
var code = str.charCodeAt(i); | |
if (code === SLASH) { | |
var codeNext = i + 1 < str.length ? str.charCodeAt(i + 1) : 0; | |
if (codeNext === STAR) { | |
result.push([1, code, i + 1, 1, 1]); | |
i++; | |
continue; | |
} | |
} | |
if (code >= ZERO && code <= NINE) { | |
result.push([2, code, i + 2, 1, 1]); | |
} else if (code === DOUBLE_QUOTE || code === QUOTE) { | |
result.push([3, code, i + 3, 1, 1]); | |
} else if (code === SPACE || code === N || code === R || code === TAB || code === F) { | |
result.push([4, code, i + 4, 1, 1]); | |
} else if (code in PUNCTUATION) { | |
result.push([6, PUNCTUATION[code], i + 6, 1, 1]); | |
} else { | |
result.push([5, code, i + 5, 1, 1]); | |
} | |
} | |
return result; | |
} | |
function testToken2(token) { | |
switch (token.type) { | |
case 1: | |
return token[3]; | |
case 2: | |
return token[2]; | |
default: | |
if (token.type === 9) { | |
return token[2]; | |
} | |
} | |
return token[4]; | |
} | |
function testSum2(tokens, fn) { | |
var res = 0; | |
for (var i = 0; i < tokens.length; i++) { | |
res += fn(tokens[i]); | |
} | |
return res; | |
} | |
function test3(str) { | |
function createToken(type, value, line, column, offset) { | |
return { type: type, value: value, line: line, column: column, offset: offset }; | |
} | |
var result = []; | |
for (var i = 0; i < str.length; i++) { | |
var code = str.charCodeAt(i); | |
if (code === SLASH) { | |
var codeNext = i + 1 < str.length ? str.charCodeAt(i + 1) : 0; | |
if (codeNext === STAR) { | |
result.push(createToken(1, code, i + 1, 1, 1)); | |
i++; | |
continue; | |
} | |
} | |
if (code >= ZERO && code <= NINE) { | |
result.push(createToken(2, code, i + 2, 1, 1)); | |
} else if (code === DOUBLE_QUOTE || code === QUOTE) { | |
result.push(createToken(3, code, i + 3, 1, 1)); | |
} else if (code === SPACE || code === N || code === R || code === TAB || code === F) { | |
result.push(createToken(4, code, i + 4, 1, 1)); | |
} else if (code in PUNCTUATION) { | |
result.push(createToken(6, PUNCTUATION[code], i + 6, 1, 1)); | |
} else { | |
result.push(createToken(5, code, i + 5, 1, 1)); | |
} | |
} | |
return result; | |
} | |
function testToken3(token) { | |
switch (token.type) { | |
case 1: | |
return token.column; | |
case 2: | |
return token.line; | |
default: | |
if (token.type === 3) { | |
return token.line; | |
} | |
} | |
return token.offset; | |
} | |
function testSum3(tokens, fn) { | |
var res = 0; | |
for (var i = 0; i < tokens.length; i++) { | |
res += fn(tokens[i]); | |
} | |
return res; | |
} | |
function test4(str) { | |
var result = []; | |
var type; | |
var value; | |
for (var i = 0; i < str.length; i++) { | |
var code = str.charCodeAt(i); | |
if (code === SLASH) { | |
var codeNext = i + 1 < str.length ? str.charCodeAt(i + 1) : 0; | |
if (codeNext === STAR) { | |
type = 1; | |
value = code; | |
i++; | |
continue; | |
} | |
} | |
if (code >= ZERO && code <= NINE) { | |
type = 2; | |
value = code; | |
} else if (code === DOUBLE_QUOTE || code === QUOTE) { | |
type = 3; | |
value = code; | |
} else if (code === SPACE || code === N || code === R || code === TAB || code === F) { | |
type = 4; | |
value = code; | |
} else if (code in PUNCTUATION) { | |
type = 6; | |
value = PUNCTUATION[code]; | |
} else { | |
type = 5; | |
value = code; | |
} | |
result.push({ type: type, value: value, line: i + 1, column: i + 1, offset: i }); | |
} | |
return result; | |
} | |
function testToken4(token) { | |
switch (token.type) { | |
case 1: | |
return token.column; | |
case 2: | |
return token.line; | |
default: | |
if (token.type === 3) { | |
return token.line; | |
} | |
} | |
return token.offset; | |
} | |
function testSum4(tokens, fn) { | |
var res = 0; | |
for (var i = 0; i < tokens.length; i++) { | |
res += fn(tokens[i]); | |
} | |
return res; | |
} | |
var fn = [test, test2, test3, test4]; | |
var fnSum = [testSum, testSum2, testSum3, testSum4]; | |
var fnToken = [testToken, testToken2, testToken3, testToken4]; | |
var string = [ | |
'a{}sd "sf"', | |
'j5yw/*rf8380 u~0u \n\r s ', | |
'""""sdf/348', | |
'\'asda 8 8878 87 87d\'', | |
'3n3b oy893yr89 yq398ry130 y4/' | |
].join(''); | |
for (var f = 0; f < fn.length; f++) { | |
var testFn = fn[f]; | |
var testS = fnSum[f]; | |
var testT = fnToken[f]; | |
var res = new Array(4); | |
for (var k = 0; k < 2; k++) { | |
var time = process.hrtime(); | |
for (var i = 0; i < 10000; i++) { | |
res = testFn(string); | |
} | |
var timeDiff = process.hrtime(time); | |
console.log(f + 1 + ' create ', parseInt(timeDiff[0] * 1e3 + timeDiff[1] / 1e6, 10)); | |
// testS([], function() { | |
// return 1; | |
// }); | |
time = process.hrtime(); | |
for (var i = 0; i < 150000; i++) { | |
testS(res.slice(i % 10), testT); | |
} | |
var timeDiff = process.hrtime(time); | |
console.log(f + 1 + ' read ', parseInt(timeDiff[0] * 1e3 + timeDiff[1] / 1e6, 10)); | |
} | |
console.log('---'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment