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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.127 Safari/537.36 OPR/60.3.3004.55692
Browser:
Opera 60
Operating system:
Linux
Device Platform:
Desktop
Date tested:
4 months ago
Test name
Executions per second
JSON.stringify
11743.3 Ops/sec
recursive for-loop
292507.8 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');