Keeping Record Construction Consistent

The match log processor builds its records in three places. The main parser calls the factory matchRecord(). A fix-up path copies the Owls scorekeeper’s rows in that log’s column order. A late-score path creates a record when a match starts and adds the points at the final whistle. The records hold the same three properties, so to a Python programmer they’re the same kind of record.

V8 sees it differently, and that matters to the totaling loop: V8 reuses what it learned about finding points in one record only for records that share its internal layout. In this lesson, you’ll learn what an object’s shape is, why V8 keeps one, and how to build records that share one.

Compare Records That Look the Same

In Python, the records would be dictionaries, and dictionary equality ignores key order:

Language: Python
def match_record(match_id, team, points):
    return {"id": match_id, "team": team, "points": points}

reordered = {"team": "Owls", "points": 2, "id": "M2"}
late = {"id": "M3", "team": "Falcons"}
late["points"] = 4

print(reordered == match_record("M2", "Owls", 2))
print(list(reordered), list(late))

The comparison passes, even though the key orders differ:

Language: Program Output
True
['team', 'points', 'id'] ['id', 'team', 'points']

Equality compares contents, though, and this lesson asks how the engine stores them. The JavaScript port carries over the construction, not the comparison: in JavaScript, two separate objects are never equal, whatever their contents. In matchRecord(), the object literal { id, team, points } is shorthand for { id: id, team: team, points: points }:

Language: JavaScript Filename: records.mjs
function matchRecord(id, team, points) {
  return { id, team, points };
}

const first = matchRecord("M1", "Falcons", 3);
const reordered = { team: "Owls", points: 2, id: "M2" };
const late = { id: "M3", team: "Falcons" };
late.points = 4;

console.log(Object.keys(first));
console.log(Object.keys(reordered));
console.log(Object.keys(late));

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.