ws-is-secure.mjs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { Bench } from 'tinybench'
  2. import { wsIsSecure } from '../lib/schemes.js'
  3. const benchWsIsSecure = new Bench({ name: 'wsIsSecure' })
  4. const wsComponentAttributeSecureTrue = {
  5. scheme: 'ws',
  6. secure: true,
  7. }
  8. const wsComponentAttributeSecureFalse = {
  9. scheme: 'ws',
  10. secure: false,
  11. }
  12. const wssComponent = {
  13. scheme: 'wss',
  14. }
  15. const wssComponentMixedCase = {
  16. scheme: 'Wss',
  17. }
  18. const wssComponentUpperCase = {
  19. scheme: 'WSS',
  20. }
  21. const httpComponent = {
  22. scheme: 'http',
  23. }
  24. console.assert(wsIsSecure(wsComponentAttributeSecureTrue) === true, 'wsComponentAttributeSecureTrue should be secure')
  25. console.assert(wsIsSecure(wsComponentAttributeSecureFalse) === false, 'wsComponentAttributeSecureFalse should not be secure')
  26. console.assert(wsIsSecure(wssComponent) === true, 'wssComponent should be secure')
  27. console.assert(wsIsSecure(wssComponentMixedCase) === true, 'wssComponentMixedCase should be secure')
  28. console.assert(wsIsSecure(wssComponentUpperCase) === true, 'wssComponentUpperCase should be secure')
  29. console.assert(wsIsSecure(httpComponent) === false, 'httpComponent should not be secure')
  30. benchWsIsSecure.add(JSON.stringify(wsComponentAttributeSecureFalse), function () {
  31. wsIsSecure(wsComponentAttributeSecureFalse)
  32. })
  33. benchWsIsSecure.add(JSON.stringify(wsComponentAttributeSecureTrue), function () {
  34. wsIsSecure(wsComponentAttributeSecureTrue)
  35. })
  36. benchWsIsSecure.add(JSON.stringify(wssComponent), function () {
  37. wsIsSecure(wssComponent)
  38. })
  39. benchWsIsSecure.add(JSON.stringify(wssComponentMixedCase), function () {
  40. wsIsSecure(wssComponentMixedCase)
  41. })
  42. benchWsIsSecure.add(JSON.stringify(wssComponentUpperCase), function () {
  43. wsIsSecure(wssComponentUpperCase)
  44. })
  45. benchWsIsSecure.add(JSON.stringify(httpComponent), function () {
  46. wsIsSecure(httpComponent)
  47. })
  48. await benchWsIsSecure.run()
  49. console.log(benchWsIsSecure.name)
  50. console.table(benchWsIsSecure.table())