-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfasterForEach.js
68 lines (49 loc) · 2.37 KB
/
fasterForEach.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Array.prototype.fasterForEach = function(itemProcessor, context) {
context = context || null;
var itemProcessorSource = itemProcessor.toString(),
loopHeader = '',
innerLoop = '',
argumentNames;
argumentNames = itemProcessorSource.slice(itemProcessorSource.indexOf('(') + 1, itemProcessorSource.indexOf(')'));
argumentNames = argumentNames.match(Array.prototype.fasterForEach.__fasterForEachItemProcessorMatcher__) || [];
if (argumentNames[0]) {
loopHeader = 'var ' + argumentNames[0];
innerLoop = argumentNames[0] + ' = __array__[__i__]; ';
}
if (argumentNames[1]) {
loopHeader += ', ' + argumentNames[1];
innerLoop += argumentNames[1] + ' = __i__; ';
}
if (argumentNames[2]) {
loopHeader += ', ' + argumentNames[2] + ' = __array__';
}
loopHeader += '; for ( ; __i__ < __len__; ++__i__) { if (!(__i__ in __array__)) { continue; }' + innerLoop;
itemProcessorSource = loopHeader + itemProcessorSource.substr(itemProcessorSource.indexOf('{') + 1);
(new Function('__i__', '__len__', '__array__', itemProcessorSource)).call(context, 0, this.length, this);
};
Array.prototype.fasterForEach.__fasterForEachItemProcessorMatcher__ = new RegExp('([^\s,]+)', 'g');
Array.prototype.fasterForEachEval = function(itemProcessor, context) {
context = context || null;
var itemProcessorSource = itemProcessor.toString(),
loopHeader = '',
innerLoop = '',
argumentNames;
argumentNames = itemProcessorSource.slice(itemProcessorSource.indexOf('(') + 1, itemProcessorSource.indexOf(')'));
argumentNames = argumentNames.match(Array.prototype.fasterForEachEval.__fasterForEachItemProcessorMatcher__) || [];
if (argumentNames[0]) {
loopHeader = 'var ' + argumentNames[0];
innerLoop = argumentNames[0] + ' = __array__[__i__]; ';
}
if (argumentNames[1]) {
loopHeader += ', ' + argumentNames[1];
innerLoop += argumentNames[1] + ' = __i__; ';
}
if (argumentNames[2]) {
loopHeader += ', ' + argumentNames[2] + ' = __array__';
}
loopHeader += '; for ( ; __i__ < __len__; ++__i__) { if (!(__i__ in __array__)) { continue; }' + innerLoop;
itemProcessorSource = loopHeader + itemProcessorSource.substr(itemProcessorSource.indexOf('{') + 1);
eval('var tmpFnc = function (__i__, __len__, __array__) {' + itemProcessorSource + '};');
tmpFnc.call(context, 0, this.length, this);
};
Array.prototype.fasterForEachEval.__fasterForEachItemProcessorMatcher__ = new RegExp('([^\s,]+)', 'g');