Skip to content

Commit

Permalink
lib: cache length prop in classic for loop
Browse files Browse the repository at this point in the history
Cache length prop in classic for loop for performance purpose.

Refs: nodejs#30958
  • Loading branch information
SukkaW committed Jul 6, 2020
1 parent 67ba825 commit e98c5df
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ function ClientRequest(input, options, cb) {
const keys = ObjectKeys(options.headers);
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (let i = 0; i < keys.length; i++) {
for (let i = 0, len = keys.length; i < len; i++) {
const key = keys[i];
this.setHeader(key, options.headers[key]);
}
Expand Down
13 changes: 7 additions & 6 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headers', {
const keys = ObjectKeys(val);
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (let i = 0; i < keys.length; ++i) {
for (let i = 0, len = keys.length; i < len; ++i) {
const name = keys[i];
headers[name.toLowerCase()] = [name, val[name]];
}
Expand All @@ -200,7 +200,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', {
const keys = ObjectKeys(headers);
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (let i = 0; i < keys.length; ++i) {
for (let i = 0, len = keys.length; i < len; ++i) {
const key = keys[i];
const val = headers[key][0];
out[key] = val;
Expand All @@ -217,7 +217,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', {
const keys = ObjectKeys(val);
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (let i = 0; i < keys.length; ++i) {
for (let i = 0, len = keys.length; i < len; ++i) {
const header = headers[keys[i]];
if (header)
header[0] = val[keys[i]];
Expand Down Expand Up @@ -471,10 +471,11 @@ function processHeader(self, state, key, value, validate) {
if (validate)
validateHeaderName(key);
if (ArrayIsArray(value)) {
if (value.length < 2 || !isCookieField(key)) {
const valueLen = value.length;
if (valueLen < 2 || !isCookieField(key)) {
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (let i = 0; i < value.length; i++)
for (let i = 0; i < valueLen; i++)
storeHeader(self, state, key, value[i], validate);
return;
}
Expand Down Expand Up @@ -577,7 +578,7 @@ OutgoingMessage.prototype.getHeaders = function getHeaders() {
const keys = ObjectKeys(headers);
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (let i = 0; i < keys.length; ++i) {
for (let i = 0, len = keys.length; i < len; ++i) {
const key = keys[i];
const val = headers[key][1];
ret[key] = val;
Expand Down
2 changes: 1 addition & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ function writeHead(statusCode, reason, obj) {
const keys = ObjectKeys(obj);
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (let i = 0; i < keys.length; i++) {
for (let i = 0, len = keys.length; i < len; i++) {
k = keys[i];
if (k) this.setHeader(k, obj[k]);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ class Http2ServerResponse extends Stream {
addTrailers(headers) {
const keys = ObjectKeys(headers);
let key = '';
for (let i = 0; i < keys.length; i++) {
for (let i = 0, len = keys.length; i < len; i++) {
key = keys[i];
this.setTrailer(key, headers[key]);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,15 +357,15 @@ function resolveExportsTarget(
throw lastException;
} else if (typeof target === 'object' && target !== null) {
const keys = ObjectGetOwnPropertyNames(target);
for (let i = 0; i < keys.length; i++) {
for (let i = 0, len = keys.length; i < len; i++) {
const key = keys[i];
if (isArrayIndex(key)) {
throw new ERR_INVALID_PACKAGE_CONFIG(
fileURLToPath(packageJSONUrl),
'"exports" cannot contain numeric property keys');
}
}
for (let i = 0; i < keys.length; i++) {
for (let i = 0, len = keys.length; i < len; i++) {
const key = keys[i];
if (key === 'default' || conditions.has(key)) {
const conditionalTarget = target[key];
Expand Down Expand Up @@ -467,7 +467,7 @@ function packageExportsResolve(

let bestMatch = '';
const keys = ObjectGetOwnPropertyNames(exports);
for (let i = 0; i < keys.length; i++) {
for (let i = 0, len = keys.length; i < len; i++) {
const key = keys[i];
if (key[key.length - 1] !== '/') continue;
if (StringPrototypeStartsWith(packageSubpath, key) &&
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class URLSearchParams {
// Need to use reflection APIs for full spec compliance.
this[searchParams] = [];
const keys = ReflectOwnKeys(init);
for (let i = 0; i < keys.length; i++) {
for (let i = 0, len = keys.length; i < len; i++) {
const key = keys[i];
const desc = ReflectGetOwnPropertyDescriptor(init, key);
if (desc !== undefined && desc.enumerable) {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,7 @@ function formatPrimitive(fn, value, ctx) {

function formatNamespaceObject(keys, ctx, value, recurseTimes) {
const output = new Array(keys.length);
for (let i = 0; i < keys.length; i++) {
for (let i = 0, len = keys.length; i < len; i++) {
try {
output[i] = formatProperty(ctx, value, recurseTimes, keys[i],
kObjectType);
Expand Down

0 comments on commit e98c5df

Please sign in to comment.