Is there a hardcoded delay in `browser. tabs.duplicate(tabId)`?

Hello,

I’ve noticed the browser.tabs.duplicate(tab.id) method takes about 400ms to complete, which is like 20 times more compare to browser.tabs.create(tab.url) which takes about 20ms.

Is there a reason for that? I know it’s not the same thing but it’s hard to believe there is a performance reason behind it related to copying tab history (even when there is none).
Try yourself:

const HighResolutionTimer = (name = '', startNow = false) => {
  let startTime, stopTime;
  if (startNow) start();
  return {
    start: start,
    stop: stop
  };
  function start() {
    startTime = performance.now();
  }
  function stop(message = '') {
    stopTime = performance.now();
    console.warn(`${name} ${message} time: ${stopTime - startTime}`);
  }
};
const tab = await browser.tabs.getCurrent();
const p = HighResolutionTimer('Speed test', true);
browser.tabs.duplicate(tab.id).then(() => p.stop('duplicate'));
browser.tabs.create({url: tab.url, active: true}).then(() => p.stop('create'));

This is because browser.tabs.duplicate() creates a new tab and copies all browsing history and session from the previous tab.

Then the internal code must be really inefficient because it takes 400ms even with a fresh new tab that has no history or session.
In a programming world 400ms is forever. Something’s not right here.

How does it compare to interactive: right-click the tab > Duplicate Tab? With this tab, the new tab appears on the bar quickly, but there are a few seconds of (re)loading.

The delays you are seeing are likely from loading the page, which we shouldn’t be waiting for, and there’s bug 1394376 to change that, with a patch posted for review, so will probably be fixed soon.

2 Likes

That does sounds very likely :slight_smile:
I will retested it once it’s merged in Nightly.
Thank you!

PS: link to the bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1394376