3

I know two ways of getting Google Chrome Extension ID:

chrome.app.getDetails().id;

chrome.i18n.getMessage('@@extension_id');

Are there any drawbacks regarding one of those?

I tend to use the first one since it's shorter but who knows. I may be wrong.

---- EDIT ----

Since I'm the only who cares, here's what I did to benchmark those:

console.time('t1');
for (var i=0; i < 10000; i++) { chrome.app.getDetails().id; }; 
console.timeEnd('t1');

console.time('t2');
for (var i=0; i < 10000; i++) { chrome.i18n.getMessage('@@extension_id'); }; 
console.timeEnd('t2');

Here are the results:

t1: 5190.766ms

t2: 860.697ms

It looks like using i18n is so much faster overall but the first one is faster at the beginning and after 3 or 4 executions, i18n is better.

François Beaufort
  • 4,843
  • 3
  • 29
  • 38

3 Answers3

3

Try to go through the similar query: How to reference the version information in a Google Chrome extension?

There you can get a broader idea of the first call, you are using.

    chrome.app.getDetails().id;
Community
  • 1
  • 1
Incredible
  • 3,495
  • 8
  • 49
  • 77
2

In the end, I created a small Chrome Extension to benchmark three known ways to retrieve Chrome Extension ID:

  • chrome.app.getDetails().id;
  • chrome.i18n.getMessage('@@extension_id');
  • chrome.extension.getURL('/').slice(19, -1);
  • chrome.runtime.id;

As you can see in the result below, chrome.extension.getURL('/').slice(19, -1); seems to spend less time overall but the first calls are equivalent to the other ones.

Benchmark image for Google Chrome 27.0.1430.0 (Official Build 186115) dev

As we can see, chrome.runtime.id wins.

Note #1: You can download the zip archive of the extension here

Note #2: I used performance.now() to get an accurate timing.

Note #3: I added chrome.runtime.id to the benchmark tool. Thanks Konstantin Smolyanin!

François Beaufort
  • 4,843
  • 3
  • 29
  • 38
0

It seems that since chrome 22 the best way to get the extension ID is built-in property chrome.runtime.id. See documentation.

console.log(chrome.runtime.id);
Konstantin Smolyanin
  • 17,579
  • 12
  • 56
  • 56