Advertisement
Guest User

Untitled

a guest
May 30th, 2016
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. 'use strict'
  2. var Measured = require('measured');
  3. var Random = require('random-js');
  4.  
  5. // set the number of elements in the arrays
  6. const elementCount = 1; // the array.find and loop joins will not complete quickly on higher numbers
  7. const sampleCount = 100;
  8. console.log('!!! ' + elementCount + ' elements with ' + sampleCount +' samples !!!');
  9.  
  10. let r = new Random();
  11. let foo = [];
  12. let bar = [];
  13. let barMap = new Map();
  14. let barMapObj = {};
  15.  
  16. for (let i = 0; i < elementCount; i++) {
  17. let _id = r.hex(24);
  18. foo[i] = {
  19. _id: _id,
  20. a: r.string(32),
  21. b: r.integer(0, 32767),
  22. c: r.bool(),
  23. d: r.date(new Date(0), new Date())
  24. };
  25.  
  26. bar[i] = {
  27. _id: _id,
  28. e: r.string(32),
  29. f: r.integer(0, 32767),
  30. g: r.bool(),
  31. h: r.date(new Date(0), new Date())
  32. };
  33. barMap.set(_id, bar[i]);
  34. barMapObj[_id] = bar[i];
  35. }
  36.  
  37. // first up array.find
  38. var arrayFindTimer = new Measured.Timer();
  39. for (let z = 0; z < sampleCount; z++) {
  40. let arrayFindSW = arrayFindTimer.start();
  41. let result = [];
  42. for (let i =0; i < foo.length; i++) {
  43. var item = bar.find(function (b) { return b._id === foo[i]._id; });
  44. result.push({
  45. _id: foo[i]._id,
  46. a: foo[i].a,
  47. b: foo[i].b,
  48. c: foo[i].c,
  49. d: foo[i].d,
  50. e: item.e,
  51. f: item.f,
  52. g: item.g,
  53. h: item.h
  54. });
  55. }
  56. arrayFindSW.end();
  57. }
  58. console.log('array.find:');
  59. console.log(arrayFindTimer.toJSON());
  60.  
  61. // double loop find is the next contender
  62. var loopFindTimer = new Measured.Timer();
  63. for (let z = 0; z < sampleCount; z++) {
  64. let loopFindSW = loopFindTimer.start();
  65. let result = [];
  66. for (let i =0; i < foo.length; i++) {
  67. let item = undefined;
  68. for (let j =0; j < bar.length; j++) {
  69. if (foo[i]._id === bar[j]._id) {
  70. item = bar[j];
  71. break;
  72. }
  73. }
  74. result.push({
  75. _id: foo[i]._id,
  76. a: foo[i].a,
  77. b: foo[i].b,
  78. c: foo[i].c,
  79. d: foo[i].d,
  80. e: item.e,
  81. f: item.f,
  82. g: item.g,
  83. h: item.h
  84. });
  85. }
  86. loopFindSW.end();
  87. }
  88. console.log('loop:');
  89. console.log(loopFindTimer.toJSON());
  90.  
  91. // merge join
  92. var mergeTimer = new Measured.Timer();
  93. for (let z = 0; z < sampleCount; z++) {
  94. let mergeSW = mergeTimer.start();
  95. let result = [];
  96. for (let i =0; i < elementCount; i++) {
  97. if (foo[i]._id === bar[i]._id) {
  98. result.push({
  99. _id: foo[i]._id,
  100. a: foo[i].a,
  101. b: foo[i].b,
  102. c: foo[i].c,
  103. d: foo[i].d,
  104. e: bar[i].e,
  105. f: bar[i].f,
  106. g: bar[i].g,
  107. h: bar[i].h
  108. });
  109. } else if (foo[i]._id < bar[i]._id) {
  110. // outer join foo
  111. result.push({
  112. _id: foo[i]._id,
  113. a: foo[i].a,
  114. b: foo[i].b,
  115. c: foo[i].c,
  116. d: foo[i].d,
  117. e: undefined,
  118. f: undefined,
  119. g: undefined,
  120. h: undefined
  121. });
  122. } else {
  123. // outer join foo
  124. result.push({
  125. _id: bar[i]._id,
  126. a: undefined,
  127. b: undefined,
  128. c: undefined,
  129. d: undefined,
  130. e: bar[i].e,
  131. f: bar[i].f,
  132. g: bar[i].g,
  133. h: bar[i].h
  134. });
  135. }
  136. }
  137. mergeSW.end();
  138. }
  139. console.log('merge:');
  140. console.log(mergeTimer.toJSON());
  141.  
  142.  
  143. // next up the ES6 map object
  144. var mapFindTimer = new Measured.Timer();
  145. for (let z = 0; z < sampleCount; z++) {
  146. let mapFindSW = mapFindTimer.start();
  147. let result = [];
  148. for (let i =0; i < foo.length; i++) {
  149. var item = barMap.get(foo[i]._id);
  150. result.push({
  151. _id: foo[i]._id,
  152. a: foo[i].a,
  153. b: foo[i].b,
  154. c: foo[i].c,
  155. d: foo[i].d,
  156. e: item.e,
  157. f: item.f,
  158. g: item.g,
  159. h: item.h
  160. });
  161. }
  162. mapFindSW.end();
  163. }
  164. console.log('map:');
  165. console.log(mapFindTimer.toJSON());
  166.  
  167. // finally the old school map objects turn
  168. var mapObjFindTimer = new Measured.Timer();
  169. for (let z = 0; z < sampleCount; z++) {
  170. let mapObjFindSW = mapObjFindTimer.start();
  171. let result = [];
  172. for (let i =0; i < foo.length; i++) {
  173. var item = barMapObj[foo[i]._id];
  174. result.push({
  175. _id: foo[i]._id,
  176. a: foo[i].a,
  177. b: foo[i].b,
  178. c: foo[i].c,
  179. d: foo[i].d,
  180. e: item.e,
  181. f: item.f,
  182. g: item.g,
  183. h: item.h
  184. });
  185. }
  186. mapObjFindSW.end();
  187. }
  188. console.log('mapObj:');
  189. console.log(mapObjFindTimer.toJSON());
  190.  
  191. process.exit(0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement