bench-urlencoded-fields-900pairs-small-alt.js 1.8 KB

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