Message build

Benchmark created on


Description

Test perf of building messages from prebuilt or build-as-you-go message object

Setup

// Mock builds
const messageBuilds = ['desktop', 'mobile'];

// Mock file paths
const mockFiles = [
    'bones/en.json',
    'bones/fr.json',
    'bones/de.json',
    'order/en.json',
    'order/fr.json',
    'order/de.json',
];

// Mock file contents
const mockFileContent = {
    'bones/en.json':
        '{"bones/postPurchaseNotification": { "desktop": { "text": "Add a year of security with an exclusive new member deal! Expires in 24 hours.", "showButton": "View deal" }, "mobile": { "text": "Newcomer deal expires in 24 hours!", "showButton": "View" }}}',
    'bones/fr.json':
        '{"bones/postPurchaseNotification": { "desktop": { "text": "Add a year of security with an exclusive new member deal! Expires in 24 hours.", "showButton": "View deal" }, "mobile": { "text": "Newcomer deal expires in 24 hours!", "showButton": "View" }}}',
    'bones/de.json':
        '{"bones/postPurchaseNotification": { "desktop": { "text": "Add a year of security with an exclusive new member deal! Expires in 24 hours.", "showButton": "View deal" }, "mobile": { "text": "Newcomer deal expires in 24 hours!", "showButton": "View" }}}',
    'order/en.json':
        '{"pagePurchase":{"title":"Enter the email for your Surfshark account","emailConfirmed":"Email confirmed","loggedIn":"Logged-in with","desktop":{"email":{"wontShareYourEmail":"We won’t share your email address with any third parties, and will only contact you when necessary to ensure service quality or to announce special offers.","invalidEmail":"Please enter a valid email address.","continueWith":"Or continue with"}},"choosePayment":"Choose a payment method","moneyBackGuarantee":"Money-back{br}guarantee","day":"day"}}',
    'order/fr.json':
        '{"pagePurchase":{"title":"Enter the email for your Surfshark account","emailConfirmed":"Email confirmed","loggedIn":"Logged-in with","desktop":{"email":{"wontShareYourEmail":"We won’t share your email address with any third parties, and will only contact you when necessary to ensure service quality or to announce special offers.","invalidEmail":"Please enter a valid email address.","continueWith":"Or continue with"}},"choosePayment":"Choose a payment method","moneyBackGuarantee":"Money-back{br}guarantee","day":"day"}}',
    'order/de.json':
        '{"pagePurchase":{"title":"Enter the email for your Surfshark account","emailConfirmed":"Email confirmed","loggedIn":"Logged-in with","desktop":{"email":{"wontShareYourEmail":"We won’t share your email address with any third parties, and will only contact you when necessary to ensure service quality or to announce special offers.","invalidEmail":"Please enter a valid email address.","continueWith":"Or continue with"}},"choosePayment":"Choose a payment method","moneyBackGuarantee":"Money-back{br}guarantee","day":"day"}}',
};

// Mock Helper Functions
const basename = (file, ext) => file.replace(ext, '');

const isObject = o =>
    typeof o === 'object' && o !== null && !Array.isArray(o);
const appendProp = (currentProp, prop) =>
    (currentProp ? `${currentProp}.${prop}` : prop);

const { entries, assign } = Object;

const flattenObject = (value, currentProp) => {
    if (!isObject(value)) return { [currentProp]: value };

    return entries(value).reduce(
        (o, [prop, val]) => ({
            ...o,
            ...flattenObject(val, appendProp(currentProp, prop)),
        }),
        {},
    );
};

const filterBuilds = (value, build, builds) => {
    if (!isObject(value)) return value;

    return entries(value).reduce((o, [key, val]) => {
        if (isObject(val)) {
            if (key === build) assign(o, filterBuilds(val, build, builds));
            else if (!builds.includes(key))
                assign(o, { [key]: filterBuilds(val, build, builds) });
        } else assign(o, { [key]: val });

        return o;
    }, {});
};

const files = mockFiles;

const locales = [...new Set(files.map(file => basename(file, '.json')))];

const prebuiltMessagesObject = Object.fromEntries(
    locales.map(locale => [
        locale,
        Object.fromEntries(
            messageBuilds.map(build => [build, {}]),
        ),
    ]),
);

Test runner

Ready to run.

Testing in
TestOps/sec
Old
const messagesObject = {};

files.forEach(file => {
    const locale = basename(file, '.json');
    const fileContent = mockFileContent[file];
    const content = JSON.parse(fileContent);

    let o = (messagesObject[locale] = messagesObject[locale] || {});

    messageBuilds.forEach(build => {
        o = messagesObject[locale][build] = messagesObject[locale][build] || {};
        assign(o, flattenObject(filterBuilds(content, build, messageBuilds)));
    });
});
ready
New
files.forEach(file => {
    const locale = basename(file, '.json');
    const fileContent = mockFileContent[file];
    const content = JSON.parse(fileContent);

    messageBuilds.forEach(build => {
        const filtered = flattenObject(filterBuilds(content, build, messageBuilds));
        assign(prebuiltMessagesObject[locale][build], filtered);
    });
});
ready
New with object build included
const newLocales = [...new Set(files.map(file => basename(file, '.json')))];

const newPrebuiltMessagesObject = Object.fromEntries(
    locales.map(locale => [
        locale,
        Object.fromEntries(
            messageBuilds.map(build => [build, {}]),
        ),
    ]),
);

files.forEach(file => {
    const locale = basename(file, '.json');
    const fileContent = mockFileContent[file];
    const content = JSON.parse(fileContent);

    messageBuilds.forEach(build => {
        const filtered = flattenObject(filterBuilds(content, build, messageBuilds));
        assign(newPrebuiltMessagesObject[locale][build], filtered);
    });
});
ready

Revisions

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