Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve performance of cast$expr #11388

Merged
merged 1 commit into from
Feb 15, 2022
Merged

Conversation

Uzlopak
Copy link
Collaborator

@Uzlopak Uzlopak commented Feb 13, 2022

Dont know if this is improving the overall performance, but should I improved some parts.

omitUndefined is now about 5 - 10 % faster
isPath is now about 40 % faster.

Benchmarks and their result:

'use strict';

const Benchmark = require('benchmark');


function omitUndefinedVar1(val) {
  const keys = Object.keys(val);
  for (const key of keys) {
    if (val[key] === void 0) {
      delete val[key];
    }
  }
}

function omitUndefinedVar2(val) {
  const keys = Object.keys(val);
  for (let i = 0, len = keys.length; i < len; ++i) {
    (val[keys[i]] === void 0) && delete val[keys[i]];
  }
}

new Benchmark.Suite()
.add('var1 valid', function () {
  const a = omitUndefinedVar1({
    a: undefined,
    b: null,
    c: undefined,
    d: 'a',
    fa: 234,
    ferfe:234234,
    adcverg:234234
  })
})
.add('var1 valid2', function () {
  const a = omitUndefinedVar1({
    d: 'a',
    fa: 234,
    ferfe:234234,
    adcverg:234234
  })
})
  .add('var2 valid', function () {
    const a = omitUndefinedVar2({
      a: undefined,
      b: null,
      c: undefined,
      d: 'a',
      fa: 234,
      ferfe:234234,
      adcverg:234234
    })
  })
  .add('var2 valid2', function () {
    const a = omitUndefinedVar2({
      d: 'a',
      fa: 234,
      ferfe:234234,
      adcverg:234234
    })
  })
  .on('cycle', function (evt) {
    if (process.env.MONGOOSE_DEV || process.env.PULL_REQUEST) {
      console.log(String(evt.target));
    }
  })
  .run();
process.memoryUsage();

// --trace-opt --trace-deopt --trace-bailout

MONGOOSE_DEV=1 node omitUndefined.js
var1 valid x 1,925,657 ops/sec ±0.56% (93 runs sampled)
var1 valid2 x 19,595,175 ops/sec ±2.95% (90 runs sampled)
var2 valid x 2,026,454 ops/sec ±0.30% (95 runs sampled)
var2 valid2 x 21,847,207 ops/sec ±0.43% (90 runs sampled)

'use strict';

const Benchmark = require('benchmark');

function isPathVar1(val) {
  return typeof val === 'string' && val.startsWith('$');
}
function isPathVar2(val) {
  return typeof val === 'string' && val[0] === '$';
}
function isPathVar3(val) {
  return typeof val === 'string' && val.charCodeAt(0) === 36;
}
function isPathVar4(val) {
  return typeof val === 'string' && val.charCodeAt() === 36;
}
function isPathVar5(val) {
  return typeof val === 'string' && val.charAt() === '$';
}
new Benchmark.Suite()
  .add('var1 valid', function () {
    const a = isPathVar1('$aasdasdf')
  })
  .add('var1 invalid', function () {
    const a = isPathVar1('aasdasdf')
  })
  .add('var2 valid', function () {
    const a = isPathVar2('$aasdasdf')
  })
  .add('var2 invalid', function () {
    const a = isPathVar2('aasdasdf')
  })
  .add('var3 valid', function () {
    const a = isPathVar3('$aasdasdf')
  })
  .add('var3 invalid', function () {
    const a = isPathVar3('aasdasdf')
  })
  .add('var4 valid', function () {
    const a = isPathVar4('$aasdasdf')
  })
  .add('var4 invalid', function () {
    const a = isPathVar4('aasdasdf')
  })
  .add('var5 valid', function () {
    const a = isPathVar5('$aasdasdf')
  })
  .add('var5 invalid', function () {
    const a = isPathVar5('aasdasdf')
  })
  .on('cycle', function (evt) {
    if (process.env.MONGOOSE_DEV || process.env.PULL_REQUEST) {
      console.log(String(evt.target));
    }
  })
  .run();
process.memoryUsage();

// --trace-opt --trace-deopt --trace-bailout

MONGOOSE_DEV=1 node isPath.js
var1 valid x 894,729,315 ops/sec ±2.38% (85 runs sampled)
var1 invalid x 901,086,157 ops/sec ±2.30% (84 runs sampled)
var2 valid x 1,373,966,160 ops/sec ±0.21% (95 runs sampled)
var2 invalid x 1,376,078,120 ops/sec ±0.13% (93 runs sampled)
var3 valid x 914,513,098 ops/sec ±2.31% (86 runs sampled)
var3 invalid x 902,081,446 ops/sec ±2.33% (85 runs sampled)
var4 valid x 906,717,792 ops/sec ±2.29% (84 runs sampled)
var4 invalid x 914,859,437 ops/sec ±2.32% (86 runs sampled)
var5 valid x 523,148,950 ops/sec ±6.62% (87 runs sampled)
var5 invalid x 136,384,194 ops/sec ±3.14% (84 runs sampled)

Variant 2 is the chosen one. We should not use startsWith when we only want to check if the first character is a specific one.

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

@vkarpov15 vkarpov15 added this to the 6.2.2 milestone Feb 15, 2022
@vkarpov15 vkarpov15 merged commit 1d5aebc into Automattic:master Feb 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants