JSON Stringify (v2)

Revision 2 of this benchmark created on


Description

how slow is JSON.stringify?

Setup

// find length via stringify
function stringifyCheck(a, b) {
    return JSON.stringify(a).length >= JSON.stringify(b).length;
}

// find length via object.keys
function lengthOf(item) {
    switch (typeof item) {
        case 'string':
            return item.length;
        case 'number':
            return 1;
        case 'object':
            if (!item) return 0;
            if (Array.isArray(item)) {
                return item.reduce((acc, item) => acc + lengthOf(item), 0);
            }
            return Object.keys(item).reduce(
                (acc, key) =>
                    acc + key.length + lengthOf(item[key]),
                0,
            );
        default:
            return 0;
    }
}

function objectKeysCheck(a, b) {
    return lengthOf(a) >= lengthOf(b);
}

// test data
var foo = {
    sectionTitle: 'Our Contributors',
    sectionDescription: 'Meet the talented team of writers, analysts, and experts who make Defector possible.',
    itemSubtitle1: 'Sports Journalist',
    itemTitle1: 'Joe Smith',
    itemDescription1: 'Joe is a veteran sports journalist with over 10 years of experience covering the NFL, NBA, and MLB. He has written for several major publications and is known for his in-depth analysis and insightful commentary.',
    itemSubtitle2: 'Data Analyst',
    itemTitle2: 'Jane Doe',
    itemDescription2: 'Jane is a data analyst with a background in statistics and computer science. She uses her skills to provide detailed analysis and visualizations of sports data, helping readers gain a deeper understanding of the game.',
    itemSubtitle3: 'Sports Historian',
    itemTitle3: 'John Johnson',
    itemDescription3: 'John is a sports historian with a Ph.D. in American Studies. He has written several books on the history of sports and is a frequent guest on sports talk shows, providing historical context and perspective.',
};

var bar = {
    heading: 'New Moon Natural Foods: Your Community Market',
    subheading: 'Welcome to a Healthier Way of Life',
    description: 'At New Moon Natural Foods, we are committed to providing our community with the highest quality organic produce, locally-sourced meats, and carefully curated health and wellness products. We also offer a unique selection of beer, wine, and cheese, as well as all the best in natural grocery. Our family-owned market is dedicated to supporting local growers and artisans, while providing a welcoming and supportive environment for our customers to find the healthiest and most sustainable options for their families. Join us on your journey to a healthier way of life.',
};

Test runner

Ready to run.

Testing in
TestOps/sec
JSON.stringify()
var isFooAsBigOrBigger = stringifyCheck(foo, bar);
ready
Object.keys()
var isFooAsBigOrBigger = objectKeysCheck(foo, bar);
ready

Revisions

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