Update index.js to improve performance #1

Open
wants to merge 3 commits into
from

Conversation

Projects
None yet
3 participants
@n074v41l4bl34u

Main equality comparison changed to use bit-wise operators.
During testing it was only few ms faster over 1000000 iterations but still ;)

n074v41l4bl34u added some commits Feb 1, 2013

Update index.js to improve performance
Main equality comparison changed to use bit-wise operators.
It is only few ms faster over 1000000 iterations but still ;)
Update index.js to include comments
Added comments describing the bit-wise comparison.
Update index.js
fixed comments misalignment
@andrewrk

This comment has been minimized.

Show comment Hide comment
@andrewrk

andrewrk Feb 3, 2013

Ew, no thanks.

andrewrk commented Feb 3, 2013

Ew, no thanks.

@fanatid

This comment has been minimized.

Show comment Hide comment
@fanatid

fanatid Mar 21, 2016

@n074v41l4bl34u
now it's not faster

var benchmark = require('benchmark')

var a = new Buffer(1024)
var b = new Buffer(a)

function node () {
  return a.equals(b)
}

function eq () {
  for (var i = 0; i < a.length; i++) {
    if (a[i] !== b[i]) return false
  }

  return true
}

function xor1 () {
  for (var i = 0; i < a.length; i++) {
    if (a[i] ^ b[i]) return false
  }

  return true
}

function xor2 () {
  for (var i = 0; i < a.length; i++) {
    if ((a[i] ^ b[i]) | 0) return false
  }

  return true
}

new benchmark.Suite()
  .on('cycle', (event) => console.log(String(event.target)))
  .add('node', node)
  .add('===', eq)
  .add('^', xor1)
  .add('^ with |0', xor2)
  .run()

results:

$ node -v
v5.7.1
$ node b.js 
node x 4,901,428 ops/sec ±0.37% (86 runs sampled)
=== x 457,491 ops/sec ±0.14% (81 runs sampled)
^ x 403,251 ops/sec ±0.08% (90 runs sampled)
^ with |0 x 403,509 ops/sec ±0.05% (88 runs sampled)

fanatid commented Mar 21, 2016

@n074v41l4bl34u
now it's not faster

var benchmark = require('benchmark')

var a = new Buffer(1024)
var b = new Buffer(a)

function node () {
  return a.equals(b)
}

function eq () {
  for (var i = 0; i < a.length; i++) {
    if (a[i] !== b[i]) return false
  }

  return true
}

function xor1 () {
  for (var i = 0; i < a.length; i++) {
    if (a[i] ^ b[i]) return false
  }

  return true
}

function xor2 () {
  for (var i = 0; i < a.length; i++) {
    if ((a[i] ^ b[i]) | 0) return false
  }

  return true
}

new benchmark.Suite()
  .on('cycle', (event) => console.log(String(event.target)))
  .add('node', node)
  .add('===', eq)
  .add('^', xor1)
  .add('^ with |0', xor2)
  .run()

results:

$ node -v
v5.7.1
$ node b.js 
node x 4,901,428 ops/sec ±0.37% (86 runs sampled)
=== x 457,491 ops/sec ±0.14% (81 runs sampled)
^ x 403,251 ops/sec ±0.08% (90 runs sampled)
^ with |0 x 403,509 ops/sec ±0.05% (88 runs sampled)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment