在 JavaScript 中剥离部分字符串时哪种方式更快

Which way is faster when stripping part of string in JavaScript

我需要剥离部分 JWT 令牌,我很好奇哪个更快,或者内部更不复杂。

示例输入字符串:

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

哪种方法更快?

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

我没有找到任何关于这些方法速度的信息,我也不是很擅长做测试:D

对于这样的事情,测量执行时间毫无意义,但是,使用字符串函数很可能会胜过您的两个示例

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

console.log(input.substr(input.indexOf('.') + 1));