Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

localstorage in iframe of different domain using postMessage

My question has to do with the performance setbacks of using localStorage and a possible workaround. As I understand it (and I'm not too sure about this), as soon as a reference to localStorage is seen on a page (compile time?) it blocks the thread and reads the data from disk to populate the localStorage memory for reference. Or maybe it looks for data on disk if the domain has stored data in the past and so it knows localstorage is used there already.

In either case, since the localstorage api is synchronous it blocks the thread to read from disk leaving us to wait for the data to be read before the page can do anything else. Say we insert an iframe into the page, which is dynamically loaded with a different domain as its source. If we use this iframe to do all the localstorage reading and writing via postMessage as described in this post, wouldn't the thread only be blocked as soon as we load the iframe? Does this give us an asynchronous method of using localstorage? Or am I totally off?

like image 494
Dirk Smaverson Avatar asked Sep 17 '12 23:09

Dirk Smaverson


1 Answers

Since every window and iframe runs in a separate browser execution context (i.e. they all execute concurrently) -- then yes, this should will you async localStorage via postMessage. Obviously, loading the iframe from a different domain versus the same domain may have impact on the security of the system. While it is true that postMessage works cross-domain, I'd rather load this iframe from a domain I completely trusted (ie. my own domain).

like image 186
Ivan Zuzak Avatar answered Oct 26 '22 15:10

Ivan Zuzak