ÿØÿàJFIFÿÛ„ ( %"1"%)+...383,7(-.- 404 Not Found
Sh3ll
OdayForums


Server : Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.20
System : Linux st2.domain.com 3.10.0-1127.10.1.el7.x86_64 #1 SMP Wed Jun 3 14:28:03 UTC 2020 x86_64
User : apache ( 48)
PHP Version : 7.4.20
Disable Function : NONE
Directory :  /home/real/node-v13.0.1/benchmark/buffers/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //home/real/node-v13.0.1/benchmark/buffers/buffer-swap.js
'use strict';

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

const bench = common.createBenchmark(main, {
  aligned: ['true', 'false'],
  method: ['swap16', 'swap32', 'swap64'/* , 'htons', 'htonl', 'htonll' */],
  len: [64, 256, 768, 1024, 2056, 8192],
  n: [1e6]
});

// The htons and htonl methods below are used to benchmark the
// performance difference between doing the byteswap in pure
// javascript regardless of Buffer size as opposed to dropping
// down to the native layer for larger Buffer sizes. Commented
// out by default because they are slow for big buffers. If
// re-evaluating the crossover point, uncomment those methods
// and comment out their implementations in lib/buffer.js so
// C++ version will always be used.

function swap(b, n, m) {
  const i = b[n];
  b[n] = b[m];
  b[m] = i;
}

Buffer.prototype.htons = function htons() {
  if (this.length % 2 !== 0)
    throw new RangeError();
  for (let i = 0; i < this.length; i += 2) {
    swap(this, i, i + 1);
  }
  return this;
};

Buffer.prototype.htonl = function htonl() {
  if (this.length % 4 !== 0)
    throw new RangeError();
  for (var i = 0; i < this.length; i += 4) {
    swap(this, i, i + 3);
    swap(this, i + 1, i + 2);
  }
  return this;
};

Buffer.prototype.htonll = function htonll() {
  if (this.length % 8 !== 0)
    throw new RangeError();
  for (let i = 0; i < this.length; i += 8) {
    swap(this, i, i + 7);
    swap(this, i + 1, i + 6);
    swap(this, i + 2, i + 5);
    swap(this, i + 3, i + 4);
  }
  return this;
};

function createBuffer(len, aligned) {
  len += aligned ? 0 : 1;
  const buf = Buffer.allocUnsafe(len);
  for (let i = 1; i <= len; i++)
    buf[i - 1] = i;
  return aligned ? buf : buf.slice(1);
}

function genMethod(method) {
  const fnString = `
      return function ${method}(n, buf) {
        for (var i = 0; i <= n; i++)
          buf.${method}();
      }`;
  return (new Function(fnString))();
}

function main({ method, len, n, aligned = 'true' }) {
  const buf = createBuffer(len, aligned === 'true');
  const bufferSwap = genMethod(method || 'swap16');

  bufferSwap(n, buf);
  bench.start();
  bufferSwap(n, buf);
  bench.end(n);
}

ZeroDay Forums Mini