bench-urlencoded-fields-100pairs-small.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. const buffers = [
  3. Buffer.from(
  4. (new Array(100)).fill('').map((_, i) => `key${i}=value${i}`).join('&')
  5. ),
  6. ];
  7. const calls = {
  8. field: 0,
  9. end: 0,
  10. };
  11. let n = 3e3;
  12. const moduleName = process.argv[2];
  13. switch (moduleName) {
  14. case 'busboy': {
  15. const busboy = require('busboy');
  16. console.time(moduleName);
  17. (function next() {
  18. const parser = busboy({
  19. limits: {
  20. fieldSizeLimit: Infinity,
  21. },
  22. headers: {
  23. 'content-type': 'application/x-www-form-urlencoded; charset=utf-8',
  24. },
  25. });
  26. parser.on('field', (name, val, info) => {
  27. ++calls.field;
  28. }).on('close', () => {
  29. ++calls.end;
  30. if (--n === 0)
  31. console.timeEnd(moduleName);
  32. else
  33. process.nextTick(next);
  34. });
  35. for (const buf of buffers)
  36. parser.write(buf);
  37. parser.end();
  38. })();
  39. break;
  40. }
  41. case 'formidable': {
  42. const QuerystringParser =
  43. require('formidable/src/parsers/Querystring.js');
  44. console.time(moduleName);
  45. (function next() {
  46. const parser = new QuerystringParser();
  47. parser.on('data', (obj) => {
  48. ++calls.field;
  49. }).on('end', () => {
  50. ++calls.end;
  51. if (--n === 0)
  52. console.timeEnd(moduleName);
  53. else
  54. process.nextTick(next);
  55. });
  56. for (const buf of buffers)
  57. parser.write(buf);
  58. parser.end();
  59. })();
  60. break;
  61. }
  62. case 'formidable-streaming': {
  63. const QuerystringParser =
  64. require('formidable/src/parsers/StreamingQuerystring.js');
  65. console.time(moduleName);
  66. (function next() {
  67. const parser = new QuerystringParser();
  68. parser.on('data', (obj) => {
  69. ++calls.field;
  70. }).on('end', () => {
  71. ++calls.end;
  72. if (--n === 0)
  73. console.timeEnd(moduleName);
  74. else
  75. process.nextTick(next);
  76. });
  77. for (const buf of buffers)
  78. parser.write(buf);
  79. parser.end();
  80. })();
  81. break;
  82. }
  83. default:
  84. if (moduleName === undefined)
  85. console.error('Missing parser module name');
  86. else
  87. console.error(`Invalid parser module name: ${moduleName}`);
  88. process.exit(1);
  89. }