Explaining What V8 Learns
In this lesson, you see what V8, the JavaScript engine inside Node, learns about your code while it runs. Your profile pointed at the scoring work, and your Python teammate has a theory: JavaScript is interpreted, so every call costs the same.
That picture comes from the classic view of CPython. score.py replays the evening’s three rows a
hundred thousand times, a toy stand-in for a day of traffic:
rows = [
{"team": "Falcons", "points": 3},
{"team": "Owls", "points": 2},
{"team": "Falcons", "points": 4},
]
def add_points(total, points):
return total + points
total = 0
for _ in range(100_000):
for row in rows:
total = add_points(total, row["points"])
print(total)
Each row’s points go through add_points(), and dis shows its addition as one BINARY_OP
instruction. The script prints the total:
900000
In JavaScript, the rows are object literals in an array, and addPoints() is a function
declaration. JavaScript has no range(); here a while loop counts n up to 100000, around
a for...of loop over the rows: