Sha256: c4642853c7793e87e2b586d7882a061941d8baad6510cbc9476bc58e9841f330

Contents?: true

Size: 910 Bytes

Versions: 5

Compression:

Stored size: 910 Bytes

Contents

'use strict';

const common = require('../common.js');
const assert = require('assert');

const bench = common.createBenchmark(main, {
  method: ['swap', 'destructure'],
  millions: [100]
});

function runSwapManual(n) {
  var i = 0, x, y, r;
  bench.start();
  for (; i < n; i++) {
    x = 1, y = 2;
    r = x;
    x = y;
    y = r;
    assert.strictEqual(x, 2);
    assert.strictEqual(y, 1);
  }
  bench.end(n / 1e6);
}

function runSwapDestructured(n) {
  var i = 0, x, y;
  bench.start();
  for (; i < n; i++) {
    x = 1, y = 2;
    [x, y] = [y, x];
    assert.strictEqual(x, 2);
    assert.strictEqual(y, 1);
  }
  bench.end(n / 1e6);
}

function main(conf) {
  const n = +conf.millions * 1e6;

  switch (conf.method) {
    case 'swap':
      runSwapManual(n);
      break;
    case 'destructure':
      runSwapDestructured(n);
      break;
    default:
      throw new Error('Unexpected method');
  }
}

Version data entries

5 entries across 4 versions & 1 rubygems

Version Path
node-compiler-0.9.1 vendor/node/benchmark/es/destructuring-bench.js
node-compiler-0.9.0 vendor/node-v7.2.1/benchmark/es/destructuring-bench.js
node-compiler-0.8.0 vendor/node-v7.2.0/benchmark/es/destructuring-bench.js
node-compiler-0.7.0 vendor/node-v6.9.1/benchmark/es/destructuring-bench.js
node-compiler-0.7.0 vendor/node-v7.1.0/benchmark/es/destructuring-bench.js