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

Check topic faster in publish handler #298

Merged
merged 1 commit into from
Aug 12, 2019

Conversation

gnought
Copy link
Collaborator

@gnought gnought commented Aug 11, 2019

No description provided.

Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Once upon a time, the previous code was faster because it traversed the string only once.

How did you measure the new implementation is faster?

@gnought
Copy link
Collaborator Author

gnought commented Aug 11, 2019

What your test result?

My environment: OSX 10.14.6, 2.3GHz Intel Core i5, 8GB 2133MHz LPDDR3
My test result:

Running node v8.16.0 (npm v6.10.3)
RegExp#test_2_if x 13,964,706 ops/sec ±1.35% (82 runs sampled)
String#indexOf_2_if x 433,598,568 ops/sec ±0.79% (87 runs sampled)
String#match_2_if x 10,586,760 ops/sec ±1.29% (84 runs sampled)
String#for_if x 15,242,784 ops/sec ±0.63% (85 runs sampled)
String#for_switch x 25,967,567 ops/sec ±0.59% (84 runs sampled)
Fastest is String#indexOf_2_if
Running node v10.16.0 (npm v6.10.3)
RegExp#test_2_if x 20,489,608 ops/sec ±0.86% (86 runs sampled)
String#indexOf_2_if x 768,251,095 ops/sec ±1.68% (78 runs sampled)
String#match_2_if x 15,602,624 ops/sec ±1.13% (85 runs sampled)
String#for_if x 53,938,379 ops/sec ±0.70% (82 runs sampled)
String#for_switch x 76,411,985 ops/sec ±0.54% (86 runs sampled)
Fastest is String#indexOf_2_if
Running node v12.6.0 (npm v6.10.3)
RegExp#test_2_if x 18,278,536 ops/sec ±0.94% (86 runs sampled)
String#indexOf_2_if x 104,353,749 ops/sec ±2.60% (77 runs sampled)
String#match_2_if x 13,024,123 ops/sec ±0.51% (81 runs sampled)
String#for_if x 32,620,111 ops/sec ±1.24% (80 runs sampled)
String#for_switch x 43,571,004 ops/sec ±1.26% (81 runs sampled)
Fastest is String#indexOf_2_if

The following is my test code:

var Benchmark = require('benchmark')
var suite = new Benchmark.Suite()

// add tests
suite.add('RegExp#test_2_if', function () {
  if (/a/.test('Hello World!')) {
    return
  }
  if (/r/.test('Hello World!')) {
    return
  }
})
.add('String#indexOf_2_if', function () {
  if ('Hello World!'.indexOf('a') > -1) {
    return
  }
  if ('Hello World!'.indexOf('r') > -1) {
    return
  }
})
.add('String#match_2_if', function () {
  if ('Hello World!'.match(/a/)) {
    return
  }
  if ('Hello World!'.match(/r/)) {
    return
  }
})
.add('String#for_if', function () {
  var s = 'Hello World!'
  for (var i = 0; i < s.length; i++) {
    if (s.charCodeAt(i) === 65) {
      return
    }
    if (s.charCodeAt(i) === 111) {
      return
    }
  }
})
.add('String#for_switch', function () {
  var s = 'Hello World!'
  for (var i = 0; i < s.length; i++) {
    switch (s.charCodeAt(i)) {
      case 65:
        return
      case 111:
        return
    }
  }
})
// add listeners
.on('cycle', function (event) {
  console.log(String(event.target))
})
.on('complete', function () {
  console.log('Fastest is ' + this.filter('fastest').map('name'))
})
// run async
.run({ async: true })

Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM, seems this was from a way older Node.js.

@gnought gnought merged commit f83783c into moscajs:master Aug 12, 2019
@gnought gnought deleted the feature/tuned_publish_handler branch August 13, 2019 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants