简体   繁体   English

我应该缓存 firebase refs 吗?

[英]Should I cache firebase refs?

I'm developing a web app backed with firebase realtime database.我正在开发一个支持 firebase 实时数据库的网络应用程序。

The app's frontend is quite complex and there are several methods that write data to the db.该应用程序的前端非常复杂,有多种方法可以将数据写入数据库。 I have several utils that look like this:我有几个看起来像这样的实用程序:

var utils = {
    setSomething: function(id, item) {
        var myRef = firebase.database().ref('my/path');
        myRef.set(item).then(something);
    }
}

The question here is: is it okay to create a new Ref inside the method (and thereby, creating a new ref with each call) or should I "cache" the ref somewhere else (just like we cache jquery objects).这里的问题是:是否可以在方法内部创建一个新的 Ref(从而在每次调用时创建一个新的 ref),或者我应该将 ref“缓存”在其他地方(就像我们缓存 jquery 对象一样)。

I could do something like this first:我可以先做这样的事情:

var cachedRefs = {
    myRef: firebase.database().ref('my/path'),
    yourRef: firebase.database().ref('your/path'),
    herRef: firebase.database().ref('her/path')
}

And then the former method could be rewritten as:然后前一种方法可以重写为:

var utils = {
    setSomething: function(id, item) {
        cachedRefs.myRef.set(item).then(something);
    }
}

Is there any performance gain besides having less code repetition?除了减少代码重复之外,还有什么性能提升吗?

firebaser here火力士在这里

References just contain the location in the database.引用只包含数据库中的位置。 they are cheap.他们很便宜。

Adding the first listener to a reference requires that we start synchronizing the data, so that is as expensive as the data you listen to.将第一个侦听器添加到引用需要我们开始同步数据,因此这与您侦听的数据一样昂贵。 Adding extra listeners is then relatively cheap, since we de-duplicate the data synchronization across listeners.添加额外的侦听器相对便宜,因为我们对侦听器之间的数据同步进行了重复数据删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM