11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common.js');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
71cb0ef41Sopenharmony_ci  method: ['swap', 'destructure'],
81cb0ef41Sopenharmony_ci  n: [1e8],
91cb0ef41Sopenharmony_ci});
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cifunction runSwapManual(n) {
121cb0ef41Sopenharmony_ci  let x, y, r;
131cb0ef41Sopenharmony_ci  bench.start();
141cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i++) {
151cb0ef41Sopenharmony_ci    x = 1;
161cb0ef41Sopenharmony_ci    y = 2;
171cb0ef41Sopenharmony_ci    r = x;
181cb0ef41Sopenharmony_ci    x = y;
191cb0ef41Sopenharmony_ci    y = r;
201cb0ef41Sopenharmony_ci    assert.strictEqual(x, 2);
211cb0ef41Sopenharmony_ci    assert.strictEqual(y, 1);
221cb0ef41Sopenharmony_ci  }
231cb0ef41Sopenharmony_ci  bench.end(n);
241cb0ef41Sopenharmony_ci}
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_cifunction runSwapDestructured(n) {
271cb0ef41Sopenharmony_ci  let x, y;
281cb0ef41Sopenharmony_ci  bench.start();
291cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i++) {
301cb0ef41Sopenharmony_ci    x = 1;
311cb0ef41Sopenharmony_ci    y = 2;
321cb0ef41Sopenharmony_ci    [x, y] = [y, x];
331cb0ef41Sopenharmony_ci    assert.strictEqual(x, 2);
341cb0ef41Sopenharmony_ci    assert.strictEqual(y, 1);
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci  bench.end(n);
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_cifunction main({ n, method }) {
401cb0ef41Sopenharmony_ci  switch (method) {
411cb0ef41Sopenharmony_ci    case 'swap':
421cb0ef41Sopenharmony_ci      runSwapManual(n);
431cb0ef41Sopenharmony_ci      break;
441cb0ef41Sopenharmony_ci    case 'destructure':
451cb0ef41Sopenharmony_ci      runSwapDestructured(n);
461cb0ef41Sopenharmony_ci      break;
471cb0ef41Sopenharmony_ci    default:
481cb0ef41Sopenharmony_ci      throw new Error(`Unexpected method "${method}"`);
491cb0ef41Sopenharmony_ci  }
501cb0ef41Sopenharmony_ci}
51