Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. function a(input) {
  2. var index = [];
  3. var item;
  4. for (var i=0; i<input.length; i++) {
  5. item = input[i];
  6. if (index.indexOf(item) === -1) {
  7. index.push(item);
  8. }
  9. }
  10. return index;
  11. }
  12. function b(input) {
  13. var index = {};
  14. var item;
  15. for (var i=0; i<input.length; i++) {
  16. item = input[i];
  17. if (index[item] === undefined) {
  18. index[item] = true;
  19. }
  20. }
  21. return index;
  22. }
  23. function c(input) {
  24. var index = {};
  25. var item;
  26. for (var i=0; i<input.length; i++) {
  27. item = input[i];
  28. index[item] = true;
  29. }
  30. return index;
  31. }
  32.  
  33. function test(f, input, times) {
  34. for (var i=0; i<times; i++) {
  35. f(input());
  36. }
  37. }
  38.  
  39. function first(amount) {
  40. return function() {
  41. var data = [];
  42. for (var i=0; i<amount; i++) {
  43. data.push(i);
  44. }
  45. return data;
  46. }
  47. }
  48.  
  49. function random(amount, range) {
  50. return function() {
  51. var data = [];
  52. for (var i=0; i<amount; i++) {
  53. data.push(Math.floor(Math.random()*range));
  54. }
  55. return data;
  56. }
  57. }
  58.  
  59. var testAmount = 1000;
  60. var testRange = 100;
  61. var testTimes = 10000;
  62.  
  63. console.log('amount, range, times');
  64. console.log(testAmount+', '+testRange+', '+testTimes);
  65. console.log('--------------------')
  66.  
  67. console.time('a'); test(a, random(testAmount, testRange), testTimes); console.timeEnd('a');
  68. console.time('b'); test(b, random(testAmount, testRange), testTimes); console.timeEnd('b');
  69. console.time('c'); test(c, random(testAmount, testRange), testTimes); console.timeEnd('c');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement