You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+33-5
Original file line number
Diff line number
Diff line change
@@ -112,22 +112,50 @@
112
112
var items = [];
113
113
```
114
114
115
-
- For [performance reasons](http://jsperf.com/array-direct-assignment-vs-push/5) use direct assignment over Array#push
115
+
- When you are managing array length use direct assignment over Array#push for [performance reasons](http://jsperf.com/array-direct-assignment-vs-push/5)
116
116
117
117
```javascript
118
-
var len = items.length,
119
-
itemsCopy = [],
118
+
var hundredOdds = [],
120
119
i;
121
120
122
121
// bad
123
-
for (i = 0; i < len; i++) {
124
-
itemsCopy.push(items[i])
122
+
for (i = 0; i < 100; i++) {
123
+
hundredOdds.push(i * 2 + 1);
124
+
}
125
+
126
+
// good
127
+
for (i = 0; i < 100; i++) {
128
+
hundredOdds[i] = i * 2 + 1;
125
129
}
130
+
```
131
+
132
+
- If you don't know array length use Array#push for [performance reasons](http://jsperf.com/array-direct-assignment-vs-push/5)
133
+
134
+
```javascript
135
+
var someStack = [],
136
+
137
+
138
+
// bad
139
+
someStack[someStack.length] = 'abracadabra';
126
140
127
141
// good
142
+
someStack.push('abracadabra');
143
+
```
144
+
145
+
- When you need to copy an array use Array() constructor [performance](http://jsperf.com/converting-arguments-to-an-array/7)
0 commit comments