Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
JSON.stringify vs recursive for-loop
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0
Browser:
Firefox 132
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
JSON.stringify
374954.5 Ops/sec
recursive for-loop
8545518.0 Ops/sec
Script Preparation code:
const tree = [{ 'title': "some title", 'channel_id': '123we', 'options': [{ 'channel_id': 'abc', 'image': 'http://asdasd.com/all-inclusive-block-img.jpg', 'title': 'All-Inclusive', 'options': [{ 'channel_id': 'dsa2', 'title': 'Some Recommends', 'options': [{ 'image': 'http://www.asdasd.com', 'title': 'Sandals', 'id': '1', 'content': {} }] }] }] }, { 'title': "some title", 'channel_id': '123we', 'options': [{ 'channel_id': 'abc', 'image': 'http://asdasd.com/all-inclusive-block-img.jpg', 'title': 'All-Inclusive', 'options': [{ 'channel_id': 'dsa2', 'title': 'Some Recommends', 'options': [{ 'image': 'http://www.asdasd.com', 'title': 'Sandals', 'id': '2', 'content': {} }] }] }] } ];
Tests:
JSON.stringify
function findNestedObj(root, ID) { let item; JSON.stringify(root, (_, obj) => { if (obj && obj.id === ID) { item = obj; } return obj; }); return item; }; const result = findNestedObj(tree, '2');
recursive for-loop
function findNestedObj(root, ID) { let item; for (let i = 0; i < root.length && !item; i += 1) { if (root[i].id === ID) { item = root[i]; return item; } if (root[i].options) { item = findNestedObj(root[i].options, ID); if (item) return item; } } return item; }; const result = findNestedObj(tree, '2');