Skip to content
Swizec Teller - a geek with a hatswizec.com

JS object optimization bug in Chrome 52

That was on Friday. I still don’t know why the bug happens, but I can reproduce it. Evidence says it’s a Chrome 52 bug, not a general JavaScript bug. Neither Safari nor Firefox make it happen.

Sometimes when you call .fetch() on a Backbone model, callbacks don’t fire. There’s no JavaScript error, the REST call works, the result is vanished by the browser. Joy of joys.

2 fetches, 1 callback

You need:

  • Webpack
  • Babel 6
  • Backbone (maybe Backbone is doing it wrong?)
  • A server that can serve JSON files
  • 14 lines of code

I used create-react-app to set up a basic environment, ejected with npm run eject and added some config to Webpack to allow jQuery. Backbone depends on it. This part is not that relevant.

To show the bug itself, we need a Backbone model:

// A basic Backbone model
class BugModel extends Backbone.Model {
constructor() {
super();
this.url = "bla.json";
}
}

Now I know what you’re thinking, that’s an ES6 object and Backbone was built before ES6. Official docs never mention the new class syntax. The traditional Backbone approach works too:

var BugModel = Backbone.Model.extend({
url: "bla.json",
});

The same bug happens. I tried.

To cause the bug, we have to call .fetch() on two instances of BugModel that share the same id. The code looks like this:

let bug = new BugModel();
bug.fetch({
success: () => {
console.log("fetch 1"); // this happens
doWeirdness(bug);
},
});
function doWeirdness(bug) {
let newBug = new BugModel({ id: bug.id });
console.log("about to re-fetch");
newBug.fetch({
success: () => console.log("fetch 2", newBug), // don't get here
error: () => console.log("error"),
});
}

We create an instance of BugModel in a variable called bug, then call .fetch(). This makes an API call to /bla.json, which returns a JSON file that looks like this: {"id": 1, "hai": "hello"}.

Backbone automagically parses this JSON string and sets an id and hai attribute. This part works like a charm.

Then we call doWeirdness and pass our bug object as an attribute. Inside doWeirdness, we make a new instance of BugModel called newBug and give it the same id as bug had. This is a crucial step.

When we call fetch on this new instance, the API call happens, but the callbacks do not. 'fetch 2' never prints.

This whole sequence of events might sound redundant, but it made sense in my real codebase. Even if re-fetching objects doesn’t make sense, it should work. It works in both Safari and Firefox.

Works in Safari

Works in Firefox

Clues about why

This smells like a bug in memory optimization. Chrome thinks bug and newBug are the same object, even though they are not.

If you call fetch() twice on the same instance of BugModel, the same bug happens. (clue, part 1)

If both instances have a different id, both fetch calls fire callbacks. (clue, part 2)

Curiously, if you add a third fetch() call, that works.

newBug.fetch({
success: () => console.log("fetch 2", newBug), // don't get here
error: () => console.log("error"),
});
newBug.fetch({
success: () => console.log("fetch 3"), // prints
});

The bug didn’t happen in Chrome 51.

I am confused. ?

PS: there is a followup

Did you enjoy this article?

Published on August 10th, 2016 in Front End, Technical

Learned something new?
Want to become a high value JavaScript expert?

Here's how it works 👇

Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet 📖right away. After that you'll get thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

Start with an interactive cheatsheet 📖

Then get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career.

"Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

~ Ashish Kumar

Join over 10,000 engineers just like you already improving their JS careers with my letters, workshops, courses, and talks. ✌️

Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️