<|start_filename|>benchmark/code/for-new-array-1.js<|end_filename|>
module.exports = repeat;

function repeat(ele, num) {
  var arr = new Array(num);

  for (var i = 0; i < num; i++) {
    arr[i] = ele;
  }

  return arr;
}


<|start_filename|>benchmark/code/for-length.js<|end_filename|>
'use strict';

module.exports = repeat;

function repeat(ele, num) {
  if (num === 0) return [];

  var res = [ele];
  var len = res.length;

  if (len < num) {
    for (var i = num - 1 - len; i >= 0; i--) {
      res[i + len] = res[i % len];
    }
  }
  return res;
}


<|start_filename|>benchmark/code/while-push.js<|end_filename|>
'use strict';

module.exports = function repeat(ele, num) {
  var arr = [];

  while (num--) {
    arr.push(ele);
  }

  return arr;
};


<|start_filename|>benchmark/code/while-exp.js<|end_filename|>
'use strict';

module.exports = function repeat(ele, num) {
  var res = [ele];

  while (res.length < num) {
    res = res.concat(res);
  }

  return res.slice(0, num);
};


<|start_filename|>benchmark/code/array-map-call.js<|end_filename|>
'use strict';

module.exports = function repeat(ele, num) {
  return Array.prototype.map.call([] + Array(num + 1), function () {
    return ele;
  });
};


<|start_filename|>benchmark/code/while-concat.js<|end_filename|>
'use strict';

module.exports = function repeat(ele, num) {
  var res = [];

  while (num--) {
    res = res.concat(ele);
  }

  return res;
};


<|start_filename|>benchmark/code/for-concat.js<|end_filename|>
'use strict';

module.exports = function repeat(ele, num) {
  var arr = [];

  for (var i = 0; i < num; i++) {
    arr = arr.concat(ele);
  }

  return arr;
};


<|start_filename|>benchmark/code/new-array-map-1.js<|end_filename|>
'use strict';

module.exports = function repeat(ele, num) {
  return Array.apply([], new Array(num)).map(function (nil) {
    return ele;
  });
};


<|start_filename|>benchmark/code/new-array-map-2.js<|end_filename|>
'use strict';

module.exports = function repeat(ele, num) {
  return Array(num + 1).join(1).split('').map(function (nil) {
    return ele;
  });
};



<|start_filename|>benchmark/code/while-array-1.js<|end_filename|>
'use strict';

module.exports = function repeat(val, num) {
  var arr = [];

  while (num--) {
    arr[num] = val;
  }

  return arr;
};


<|start_filename|>benchmark/code/while-bitwise.js<|end_filename|>

module.exports = repeat;

function repeat(ele, num) {
  var arr = [ele];
  var res = [];

  while (num > 0) {
    if (num & 1) {
      res.push.apply(res, arr);
    }
    num >>= 1;
    arr.push.apply(arr, arr);
  }
  return res;
}


<|start_filename|>benchmark/code/array-map-valueOf.js<|end_filename|>
'use strict';

module.exports = function repeat(ele, num) {
  return Array.apply(null, Array(num)).map(String.prototype.valueOf, ele);
};


<|start_filename|>test.js<|end_filename|>
/*!
 * repeat-element <https://github.com/jonschlinkert/repeat-element>
 *
 * Copyright (c) 2014 <NAME>, contributors.
 * Licensed under the MIT License
 */

'use strict';

require('mocha');
var fs = require('fs');
var path = require('path');
var assert = require('assert');
var argv = require('minimist')(process.argv.slice(2));
var files = fs.readdirSync('./benchmark/code');
var repeat = require('./');

var keys = Object.keys(argv);
if (keys && keys[1]) {
  var lib = files.filter(function(fp) {
    return keys[1] === path.basename(fp, path.extname(fp));
  });
  repeat = require(path.resolve('./benchmark/code/' + lib[0]));
}

it('should repeat the given string:', function() {
  assert.deepEqual(repeat('a', 5), ['a', 'a', 'a', 'a', 'a']);
  assert.deepEqual(repeat('a', 50), ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']);
  assert.deepEqual(repeat('a', 1), ['a']);
  assert.deepEqual(repeat('a', 0), []);
});

it('should repeat the given object:', function() {
  assert.deepEqual(repeat({a: 'b'}, 5), [{a: 'b'},{a: 'b'},{a: 'b'},{a: 'b'},{a: 'b'}]);
});

it('should repeat null:', function() {
  assert.deepEqual(repeat(null, 5), [null, null, null, null, null]);
});


<|start_filename|>benchmark/code/array-from.js<|end_filename|>
'use strict';

function getThis() {
  return this;
}

module.exports = function repeat(ele, num) {
  return Array.from({length: num}, getThis, ele);
};


<|start_filename|>benchmark/check.js<|end_filename|>
'use strict';

const chalk = require('chalk');
const path = require('path');
const glob = require('glob');
const repeat = require('../');

/**
 * Sanity check. Run to ensure that all fns return the same result.
 */

let fixtures = glob.sync(__dirname + '/fixtures/*.js').map(require);
let expected = fixtures.map(fixture => repeat.apply(repeat, fixture).length);

glob.sync(__dirname + '/code/*.js').forEach(function (fp) {
  let fn = require(path.resolve(__dirname, 'code', fp));
  let name = path.basename(fp, path.extname(fp));
  let problems = [];

  fixtures.forEach(function (fixture, idx) {
    let answer = fn.apply(fn, fixture).length;

    if (answer !== expected[idx]) {
      problems.push(['repeat(' + fixture.join(', ') + ').length', answer, expected[idx]]);
    }
  });

  if (problems.length === 0) {
    console.log(' ' + chalk.bold.green('✔') + ' ' + chalk.bold(name));
  } else {
    console.log(' ' + chalk.bold.red('✖') + ' ' + chalk.bold(name));

    problems.forEach(function (item, idx, arr) {
      let str = item[0] + ' gave ' + item[1] + ', expected ' + item[2];
      console.log((idx === arr.length - 1 ? ' ┗ ' : ' ┣ ') + chalk.red(str));
    });
  }
});


<|start_filename|>benchmark/code/for-push.js<|end_filename|>
'use strict';

module.exports = function repeat(ele, num) {
  var arr = [];

  for (var i = num; i > 0; i--) {
    arr.push(ele);
  }

  return arr;
};


<|start_filename|>benchmark/code/while-stack.js<|end_filename|>
'use strict';

module.exports = function repeat(ele, num) {
  var res = [ele];
  var max = num;

  while (num--) {
    res.push.apply(res, res);
    if (res.length >= max) {
      break;
    }
  }

  return res.slice(0, max);
};


<|start_filename|>benchmark/code/for-join-2.js<|end_filename|>
'use strict';

module.exports = function repeat(ele, num) {
  var arr = [];

  for (; 0 < num; arr[--num] = ele);

  return arr;
};
