Skip to content

Commit b382546

Browse files
committed
Refactor to improve size, perf
1 parent a3ca8f6 commit b382546

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

index.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,20 @@ module.exports = size
66

77
function size(node, test) {
88
var is = convert(test)
9-
var children = node && node.children
10-
var length = (children && children.length) || 0
11-
var count = 0
12-
var index = -1
13-
var child
9+
return fastSize(node)
1410

15-
while (++index < length) {
16-
child = children[index]
11+
function fastSize(node) {
12+
var children = node && node.children
13+
var count = 0
14+
var index = -1
1715

18-
if (is(child, index, node)) {
19-
count++
16+
if (children && children.length) {
17+
while (++index < children.length) {
18+
if (is(children[index], index, node)) count++
19+
count += fastSize(children[index])
20+
}
2021
}
2122

22-
count += size(child, test)
23+
return count
2324
}
24-
25-
return count
2625
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@
6767
"esnext": false,
6868
"ignores": [
6969
"unist-util-size.js"
70-
]
70+
],
71+
"rules": {
72+
"unicorn/explicit-length-check": "off"
73+
}
7174
},
7275
"remarkConfig": {
7376
"plugins": [

0 commit comments

Comments
 (0)