JSON Stringify (v2)

Revision 2 of this benchmark created on


Description

how slow is JSON.stringify?

Setup

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.',
            };
 
function stringifyCheck(a, b) {
	return JSON.stringify(a).length >= JSON.stringify(b).length;
}

function getObjectLength(a) {
	return Object.keys(a).reduce((acc, key) => acc + key.length + a[key].length || 0, 0);
}

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

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.