-2

I need to strip part of JWT token and I am courious which one is faster, or less complex internally.

Example input string:

const input = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjMsInR5cGUiOjAsImlhdCI6MTU4MTk3NDk1MCwiZXhwIjoxNTgxOTc4NTUwfQ.oEwxI51kVjB6jJUY2N5Ct6-hO0GUCUonolPbryUo-lI"

Which one of those methods is faster?

const output =  input.split('.').slice(2,3).join('.');
const output =  input.replace("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.","");
const output =  //REGEX replace

I not found any informations about speeds of those methods and I am not exactly master in making tests :D

Baterka
  • 3,075
  • 5
  • 31
  • 60
  • It’s probably going to be one time thing dont bother over engineering performance for it – Rikin Feb 17 '20 at 22:17
  • This very much depends on the environment you're running the code it. The answer is most definitely that *it doesn't matter*. Especially if you're running it only once, the difference will be miniscule. – TimoStaudinger Feb 17 '20 at 22:18
  • 1
    Of the first two options, the first is faster by mere milliseconds. http://jsben.ch/UqLeb Without knowing your regex replace code in your third option I can't test it. As others have said, it's highly likely you should not worry about this as the difference is super small. – AndyWarren Feb 17 '20 at 22:26
  • The example methods are not producing the same result. – Titus Feb 17 '20 at 22:31

1 Answers1

3

For things like this, measuring execution time never makes sense, however, using string functions will most likely outperform both of your examples

const input = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjMsInR5cGUiOjAsImlhdCI6MTU4MTk3NDk1MCwiZXhwIjoxNTgxOTc4NTUwfQ.oEwxI51kVjB6jJUY2N5Ct6-hO0GUCUonolPbryUo-lI";

console.log(input.substr(input.indexOf('.') + 1));
baao
  • 71,625
  • 17
  • 143
  • 203