JWT Decode Memoized vs. Not

Benchmark created on


Preparation HTML

<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

Setup

class InvalidTokenError extends Error {
}
InvalidTokenError.prototype.name = "InvalidTokenError";
function b64DecodeUnicode(str) {
    return decodeURIComponent(atob(str).replace(/(.)/g, (m, p) => {
        let code = p.charCodeAt(0).toString(16).toUpperCase();
        if (code.length < 2) {
            code = "0" + code;
        }
        return "%" + code;
    }));
}
function base64UrlDecode(str) {
    let output = str.replace(/-/g, "+").replace(/_/g, "/");
    switch (output.length % 4) {
        case 0:
            break;
        case 2:
            output += "==";
            break;
        case 3:
            output += "=";
            break;
        default:
            throw new Error("base64 string is not of the correct length");
    }
    try {
        return b64DecodeUnicode(output);
    }
    catch (err) {
        return atob(output);
    }
}
function jwtDecode(token, options) {
    if (typeof token !== "string") {
        throw new InvalidTokenError("Invalid token specified: must be a string");
    }
    options || (options = {});
    const pos = options.header === true ? 0 : 1;
    const part = token.split(".")[pos];
    if (typeof part !== "string") {
        throw new InvalidTokenError(`Invalid token specified: missing part #${pos + 1}`);
    }
    let decoded;
    try {
        decoded = base64UrlDecode(part);
    }
    catch (e) {
        throw new InvalidTokenError(`Invalid token specified: invalid base64 for part #${pos + 1} (${e.message})`);
    }
    try {
        return JSON.parse(decoded);
    }
    catch (e) {
        throw new InvalidTokenError(`Invalid token specified: invalid json for part #${pos + 1} (${e.message})`);
    }
}


const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjkwMDE5NDMsInN1YiI6eyJpZCI6IjQxOGY1ZWRjLTk5MTMtNDgyNy05ZjBjLTEyZTM0M2ExMjE3MSIsImltYWdlIjpbInMzOi8vZnJhbWVpby1hc3NldHMtZGV2ZWxvcG1lbnQvaW1hZ2UvNDE4ZjVlZGMtOTkxMy00ODI3LTlmMGMtMTJlMzQzYTEyMTcxL2ltYWdlX3NtYWxsLnBuZyJdLCJzdHJlYW1fdHlwZSI6ImltYWdlIiwid2F0ZXJtYXJrIjpbeyJhbHBoYSI6MS4wLCJmb250X2NvbmZpZyI6eyJmb250X3NpemVfZmFjdG9yIjo1NCwibGluZV9oZWlnaHRfZmFjdG9yIjo2NiwicG9zaXRpb25fcGFkZGluZ194Ijo0MiwicG9zaXRpb25fcGFkZGluZ195IjozMH0sImZvbnRfc2l6ZSI6Ik1lZGl1bSIsImxpbmVzIjpbIllvdXIgQ3VzdG9tIFRleHQiXSwicm90YXRpb24iOjAuMCwic2Nyb2xsX3RleHQiOiJOb25lIiwic2hhZG93IjoiT2ZmIiwidGV4dF9hbGlnbm1lbnQiOiJDZW50ZXIiLCJ0ZXh0X2NvbG9yIjoiI0ZGRkZGRiIsIndtX2Nvb3JkaW5hdGUiOnsibmFtZSI6Ik1pZGRsZSBDZW50ZXIiLCJ4IjowLjUsInkiOjAuNX19XX19.VrcYRYR8GUzdmcKldtEf4fG2ToJakq3Rg87xO4AYiGA'

        const memoizedJwtDecode = _.memoize(jwtDecode);

        function testMemoized() {
            const decoded = memoizedJwtDecode(token);
        }

        function testNonMemoized() {
            const decoded = jwtDecode(token);
        }

Test runner

Ready to run.

Testing in
TestOps/sec
Memoized
testMemoized()
ready
Non-memoized
testNonMemoized()
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.