JSON.parse copy vs deep extend (v2)

Revision 2 of this benchmark created by rmunson on


Description

If you have a known static object, you can cache the stringified version for later parseing. Is this enough to swing object copies in the favor of JSON.parse over $.extend(true,{},{})?

Stay tuned...

Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>

var _gumboMeta = {
        "dfeEvents": null,
        "logicalEvents": null,
        "timestamp": null,
        "dispatchNumber": null,
        "wait": null
    },
    _gumboGame = {
        "game": {
            "pk": null,
            "id": null,
            "doubleHeader": null,
            "type": null,
            "gamedayType": null,
            "tiebreaker": null,
            "gameNumber": null,
            "calendarEventID": null
        },
        "teams": {
            "away": {
                "division": null,
                "fileCode": null,
                "leagueID": null,
                "sportcode": null,
                "teamID": null,
                "divisionID": null,
                "league": null,
                "name": {
                    "abbrev": null,
                    "display": null,
                    "full": null,
                    "brief": null,
                    "city": null
                },
                "record": {
                    "win": null,
                    "loss": null,
                    "gamesBack": null,
                    "gamesBackWildcard": null
                }
            },
            "home": {
                "division": null,
                "fileCode": null,
                "leagueID": null,
                "sportcode": null,
                "teamID": null,
                "divisionID": null,
                "league": null,
                "name": {
                    "abbrev": null,
                    "display": null,
                    "full": null,
                    "brief": null,
                    "city": null
                },
                "record": {
                    "win": null,
                    "loss": null,
                    "gamesBack": null,
                    "gamesBackWildcard": null
                }
            },
            "teamsByID": null
        },
        "status": {
            "abstractGamestate": null,
            "codedGamestate": null,
            "detailedState": null,
            "statusCode": null,
            "reason": null,
            "challenges": {
                "hasChallenges": null,
                "away": {
                    "used": null,
                    "remaining": null
                },
                "home": {
                    "used": null,
                    "remaining": null
                },
                "status": null,
                "challengingTeam": null
            }
        },
        "datetime": {
            "ampm": null,
            "day": null,
            "dayNight": null,
            "firstPitchET": null,
            "originalDate": null,
            "resumeDate": null,
            "time": null,
            "timeGMT": null,
            "timeDate": null,
            "timezone": null,
            "away": {
                "ampm": null,
                "time": null,
                "timezone": null,
                "league": {
                    "time": null,
                    "ampm": null,
                    "timeDate": null,
                    "timezone": null,
                    "timezoneGen": null
                }
            },
            "home": {
                "ampm": null,
                "time": null,
                "timezone": null,
                "league": {
                    "time": null,
                    "ampm": null,
                    "timeDate": null,
                    "timezone": null,
                    "timezoneGen": null
                }
            }
        },
        "stadium": {
            "id": null,
            "location": null,
            "name": null,
            "channelLoc": null
        },
        "weather": {
            "condition": null,
            "temp": null,
            "wind": null
        },
        "previewWrap": {
            "dataDirectory": null,
            "away": {
                "preview": null,
                "recap": null
            },
            "home": {
                "preview": null,
                "recap": null
            },
            "photos": null,
            "preview": null,
            "wrap": null,
            "tvStation": null,
            "media": {
                "free": null,
                "title": null,
                "thumbnail": null,
                "start": null,
                "mlbtv": null,
                "type": null
            }
        }
    },
    _gumboLive = {
        "players": {
            "away": {
                "batters": null,
                "battingOrder": null,
                "pitchers": null,
                "bench": null,
                "bullpen": null
            },
            "home": {
                "batters": null,
                "battingOrder": null,
                "pitchers": null,
                "bench": null,
                "bullpen": null
            },
            "offense": {
                "ondeck": null,
                "inhole": null,
                "currentBatter": null,
                "runners": {
                    "first": null,
                    "second": null,
                    "third": null
                },
                "teamID": null
            },
            "defense": {
                "catcher": null,
                "centerfield": null,
                "first": null,
                "leftfield": null,
                "pitcher": null,
                "rightfield": null,
                "second": null,
                "shortstop": null,
                "third": null,
                "teamID": null
            }
        },
        "plays": {
            "currentPlay": null,
            "allPlays": null,
            "playsByInning": null,
            "scoringPlays": null
        },
        "boxscore": {
            "gameInfo": null,
            "teams": {
                "away": {
                    "teamID": null,
                    "info": null,
                    "note": null,
                    "battingTotals": {
                        "atBats": null,
                        "avg": null,
                        "baseOnBalls": null,
                        "doubles": null,
                        "assists": null,
                        "hits": null,
                        "homeruns": null,
                        "leftOnBase": null,
                        "putOuts": null,
                        "runs": null,
                        "rbi": null,
                        "strikeOuts": null,
                        "triples": null
                    },
                    "pitchingTotatls": {
                        "baseOnBalls": null,
                        "battersfFaced": null,
                        "errors": null,
                        "era": null,
                        "hits": null,
                        "homeruns": null,
                        "outs": null,
                        "runs": null,
                        "strikeOuts": null
                    }
                },
                "home": {
                    "teamID": null,
                    "info": null,
                    "note": null,
                    "battingTotals": {
                        "atBats": null,
                        "avg": null,
                        "baseOnBalls": null,
                        "doubles": null,
                        "assists": null,
                        "hits": null,
                        "homeruns": null,
                        "leftOnBase": null,
                        "putOuts": null,
                        "runs": null,
                        "rbi": null,
                        "strikeOuts": null,
                        "triples": null
                    },
                    "pitchingTotatls": {
                        "baseOnBalls": null,
                        "battersfFaced": null,
                        "errors": null,
                        "era": null,
                        "hits": null,
                        "homeruns": null,
                        "outs": null,
                        "runs": null,
                        "strikeOuts": null
                    }
                }
            }
        },
        "linescore": {
            "currentInning": null,
            "inningState": null,
            "innings": null,
            "away": {
                "id": null,
                "runs": null,
                "hits": null,
                "errors": null
            },
            "home": {
                "id": null,
                "runs": null,
                "hits": null,
                "errors": null
            },
            "pitchers": {
                "win": null,
                "lose": null,
                "save": null
            }
        }
    };

var liveStr,
    gameStr,
    metaStr;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Extend
// Need a single check on props to even out comparison from the the test
_gumboMeta = _gumboMeta;
_gumboLive = _gumboLive;
_gumboGame = _gumboGame;
var gumbo = {
    metaData : $.extend(true,{},_gumboMeta),
    liveData : $.extend(true,{},_gumboLive),
    game     : $.extend(true,{},_gumboGame)
}
ready
Parse
metaStr = JSON.stringify(_gumboMeta);
liveStr = JSON.stringify(_gumboLive);
gameStr = JSON.stringify(_gumboGame);

var gumbo = {
    metaData : JSON.parse(metaStr),
    liveData : JSON.parse(liveStr),
    game     : JSON.parse(gameStr)
}
ready

Revisions

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

  • Revision 1: published by rmunson on
  • Revision 2: published by rmunson on