validate.fixture.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * Module dependencies
  3. */
  4. var _ = require('@sailshq/lodash');
  5. var parley = require('../../');
  6. /**
  7. * validate.fixture.js
  8. *
  9. * A simplified mock of a hypothetical `validate()` model method
  10. * that is actually synchronous. (This is primarily for use in benchmarks.)
  11. *
  12. * @param {Function} explicitCbMaybe
  13. *
  14. * @returns {Deferred} If no callback specified
  15. */
  16. module.exports = function validate(explicitCbMaybe){
  17. var metadata = {};
  18. // This deferred may or may not actually need to get built.
  19. // (but in case it does, we define it out here so we can unambiguously
  20. // return it below)
  21. var deferred;
  22. // If an explicit callback was specified, then go ahead
  23. // and proceed to where the real action is at.
  24. // Otherwise, no callback was specified explicitly,
  25. // so we'll build and return a Deferred instead.
  26. deferred = parley(function (finalCb){
  27. // Now actually do stuff.
  28. // ...except actually don't-- this is just pretend.
  29. // All done.
  30. return finalCb();
  31. }, explicitCbMaybe);
  32. // If we ended up building a Deferred above, we would have done so synchronously.
  33. // In other words, if there's going to be a Deferred, we have it here.
  34. //
  35. // So if we DON'T have a Deferred, then we know that we must have already went ahead
  36. // and performed our business logic. So we'll just return undefined.
  37. if (!deferred) {
  38. return;
  39. }//-•
  40. // IWMIH, then we know we have a Deferred.
  41. // (and thus we haven't actually done anything yet.)
  42. // At this point, we might opt to attach some methods to our Deferred.
  43. // --(1)-------------------------------------------------------
  44. // --too slow:
  45. // --(e.g. 212k ops/sec)
  46. // deferred.meta = function (_meta){
  47. // metadata.meta = _meta;
  48. // return deferred;
  49. // };
  50. // --(2)-------------------------------------------------------
  51. // --perfectly fast, but doesn't do anything:
  52. // --(e.g. 373k ops/sec)
  53. // var theMeta = function (_meta){
  54. // metadata.meta = _meta;
  55. // return deferred;
  56. // };
  57. // --(3)-------------------------------------------------------
  58. // --somewhat better than the original!!...
  59. // --(e.g. 273k ops/sec)
  60. // --....but problematic, because it doesn't actually mutate
  61. // --the original deferred, which could cause inconsistencies.
  62. // deferred = _.extend({
  63. // meta: function (_meta){
  64. // metadata.meta = _meta;
  65. // return deferred;
  66. // }
  67. // }, deferred);
  68. // --(4)-------------------------------------------------------
  69. // --considerably better than the original!!
  70. // --(Even more than #3... plus it's totally valid!)
  71. // --(e.g. ~268k-292k ops/sec)
  72. _.extend(deferred, {
  73. meta: function (_meta){
  74. metadata.meta = _meta;
  75. return deferred;
  76. },
  77. // Uncomment these methods for testing performance:
  78. // (this function gets slower and slower the more you add dynamically like this)
  79. // ================================================================================================
  80. // a: function (beep, boop) { console.log(Math.random()+'hi0'); return deferred; },
  81. // b: function (baa, baaa, black, sheep) { console.log(Math.random()+'hi1'); return deferred; },
  82. // c: function (beep, boop) { console.log(Math.random()+'hi2'); return deferred; },
  83. // d: function (baa, baaa, black, sheep) { console.log(Math.random()+'hi3'); return deferred; },
  84. // e: function (beep, boop) { console.log(Math.random()+'hi5'); return deferred; },
  85. // f: function (baa, baaa, black, sheep) { console.log(Math.random()+'hi5'); return deferred; },
  86. // g: function (beep, boop) { console.log(Math.random()+'hi6'); return deferred; },
  87. // h: function (baa, baaa, black, sheep) { console.log(Math.random()+'hi7'); return deferred; },
  88. // i: function (beep, boop) { console.log(Math.random()+'hi8'); return deferred; },
  89. // j: function (baa, baaa, black, sheep) { console.log(Math.random()+'hi9'); return deferred; },
  90. // k: function (beep, boop) { console.log(Math.random()+'hi10'); return deferred; },
  91. // l: function (baa, baaa, black, sheep) { console.log(Math.random()+'hi11'); return deferred; },
  92. // ================================================================================================
  93. });
  94. // When we're confident that our Deferred is ready for primetime,
  95. // we finish up by returning it.
  96. return deferred;
  97. };