11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst SlowBuffer = require('buffer').SlowBuffer;
31cb0ef41Sopenharmony_ciconst common = require('../common.js');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
71cb0ef41Sopenharmony_ci  size: [512, 4096, 16386],
81cb0ef41Sopenharmony_ci  type: ['fast'],
91cb0ef41Sopenharmony_ci  method: ['for', 'forOf', 'iterator'],
101cb0ef41Sopenharmony_ci  n: [1e3],
111cb0ef41Sopenharmony_ci});
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst methods = {
141cb0ef41Sopenharmony_ci  'for': benchFor,
151cb0ef41Sopenharmony_ci  'forOf': benchForOf,
161cb0ef41Sopenharmony_ci  'iterator': benchIterator,
171cb0ef41Sopenharmony_ci};
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_cifunction main({ size, type, method, n }) {
201cb0ef41Sopenharmony_ci  const buffer = type === 'fast' ?
211cb0ef41Sopenharmony_ci    Buffer.alloc(size) :
221cb0ef41Sopenharmony_ci    SlowBuffer(size).fill(0);
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  const fn = methods[method];
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  bench.start();
271cb0ef41Sopenharmony_ci  fn(buffer, n);
281cb0ef41Sopenharmony_ci  bench.end(n);
291cb0ef41Sopenharmony_ci}
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_cifunction benchFor(buffer, n) {
321cb0ef41Sopenharmony_ci  for (let k = 0; k < n; k++) {
331cb0ef41Sopenharmony_ci    for (let i = 0; i < buffer.length; i++) {
341cb0ef41Sopenharmony_ci      assert(buffer[i] === 0);
351cb0ef41Sopenharmony_ci    }
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_cifunction benchForOf(buffer, n) {
401cb0ef41Sopenharmony_ci  for (let k = 0; k < n; k++) {
411cb0ef41Sopenharmony_ci    for (const b of buffer) {
421cb0ef41Sopenharmony_ci      assert(b === 0);
431cb0ef41Sopenharmony_ci    }
441cb0ef41Sopenharmony_ci  }
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_cifunction benchIterator(buffer, n) {
481cb0ef41Sopenharmony_ci  for (let k = 0; k < n; k++) {
491cb0ef41Sopenharmony_ci    const iter = buffer[Symbol.iterator]();
501cb0ef41Sopenharmony_ci    let cur = iter.next();
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci    while (!cur.done) {
531cb0ef41Sopenharmony_ci      assert(cur.value === 0);
541cb0ef41Sopenharmony_ci      cur = iter.next();
551cb0ef41Sopenharmony_ci    }
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  }
581cb0ef41Sopenharmony_ci}
59