当前位置: 首页 > 编程日记 > 正文

javascript优化_如何通过使用服务人员来优化JavaScript应用

javascript优化

Every now and then we hear about optimizing something. There are different kinds of optimizations we can do to make our apps faster and more efficient, or to save time or memory. This article will cover one of those methods — service workers.

时不时地我们听到关于优化某些东西的信息。 我们可以进行多种优化,以使我们的应用程序更快,更高效,或节省时间或内存。 本文将介绍这些方法之一- 服务 工作者。

TL; DR (TL;DR)

This tutorial explains what a service worker is and how to use it, in JavaScript. There is a code example at the end of it. If you want to skip the reading, here is the Git repo and here you may see a live demo.

本教程用JavaScript解释了什么是服务工作者以及如何使用它。 在其末尾有一个代码示例。 如果你想跳过阅读, 这里是Git的回购, 在这里你可以看到现场演示。

理论 (The Theory)

Let’s see first what a worker is this 👀 and what service can we use it for 🤓.

让我们首先看到AW orker这是👀和有何ervice我们可以用它来🤓。

The service worker is a simple script. It's JavaScript code, that your browser runs in the background, separate from a web page.

服务人员是一个简单的脚本 。 这是JavaScript代码,您的浏览器在后台运行,与网页分开。

It’s very convenient to use service workers for features that don’t need a web page or user interaction. One of the most common uses is intercepting and handling network requests. This includes managing a cache of responses.

将服务工作者用于不需要网页或用户交互的功能非常方便。 最常见的用途之一是拦截和处理网络请求。 这包括管理响应的缓存。

The following is a simple example of how to include a service worker in your application.

以下是一个简单的示例,说明如何在您的应用程序中包括服务工作者。

Usually, in the entry point of your app, you add this code:

通常,在应用程序的入口点,添加以下代码:

if ('serviceWorker' in navigator) {  window.addEventListener('load', function() {navigator.serviceWorker.register('/service-worker.js');  });
}

This way of using service workers is a little bit improved than the basic one. The basic method involves directly calling the register() method inside the if statement. In this case, we use the window load event to register the service worker after the page has finished loading. After doing this, you need to add your service worker code in the service-worker.js file. At this point, you might want to take a look at my service worker file.

这种使用服务人员的方式比基本方式有所改进。 基本方法涉及直接在if语句内部调用register ()方法 在这种情况下,我们使用window load事件在页面完成加载后注册服务工作者。 完成此操作后,您需要在service-worker.js文件中添加服务工作者代码。 此时,您可能想看一下我的服务工作者文件。

All major browsers support Service Workers now, and you can start using them right away.

所有主要的浏览器现在都支持Service Worker,您可以立即开始使用它们。

这个例子 (The Example)

Enough theory, let’s build a real example app that will leverage the service workers feature.

有足够的理论,让我们构建一个真正的示例应用程序,以利用服务工作者功能。

Let’s imagine we are building an app that needs to load a big chunk of data. It could be, for example, a nice, big full-screen image we display on the front page. Or it could be a big video clip we have to wait to load. This is an ideal use case for a service worker to shine. Let’s see how. 👀

假设我们正在构建一个需要加载大量数据的应用程序。 例如,它可能是我们在首页上显示的漂亮的大全屏图像。 或者它可能是一个大视频剪辑,我们必须等待加载。 对于服务人员来说,这是一个理想的用例。 让我们看看如何。 👀

In our specific case, we will use the clock time to show the benefit of using service workers. What I mean is, that we will build a simple app, showing the time. It will have a nice, big button for fetching a nice, big image. And it will provide the user with an option to choose to use or not a service worker.

在我们的特定情况下,我们将使用时钟时间来展示使用服务工作者的好处。 我的意思是,我们将构建一个简单的应用程序来显示时间。 它将具有一个漂亮的大按钮,用于获取漂亮的大图像。 并且它将为用户提供选择使用还是不使用服务人员的选项。

Here is a screenshot of how it looks:

这是它的外观的屏幕截图:

What this app demonstrates is, that when fetching the image (by clicking the button, wow!) with an active service worker — we don’t get blocked UI (user interface, i.e. fields, buttons, 🐛). If you choose not to use the service worker, you will get a frozen UI for a certain period of time. When the work completes and the main thread frees itself, it will unfreeze the UI.

该应用程序演示的是,当与活动的服务工作者一起获取图像时(通过单击按钮,哇!),我们不会遇到阻塞的UI(用户界面,即字段,按钮,🐛)。 如果您选择不使用服务工作者,则将在一段时间内获得冻结的UI。 当工作完成并且主线程释放自身时,它将解冻UI。

If you don’t want to clone and run the code yourself, jump straight to the live demo.

如果您不想自己克隆并运行代码,请直接跳至实时演示 。

结论 (Conclusion)

This demo of service workers in action shows us the advantage we get from using them. Especially when you are trying to build responsive and robust JavaScript applications. No user wants to end up in a frozen page for an unknown time, as no developer should want that for his application’s users. Keeping in mind the above, service workers are a *must* now. And we should not neglect them.

这次服务工人的演示向我们展示了使用他们所获得的好处。 尤其是当您尝试构建响应式且功能强大JavaScript应用程序时。 没有用户希望在冻结的页面中停留一个未知的时间,因为没有开发人员应该为其应用程序的用户提供该功能。 牢记上述,服务工作者现在是*必须*。 我们不应该忽视他们。

🔥 Thanks for reading! 🔥

🔥感谢您的阅读! 🔥

翻译自: https://www.freecodecamp.org/news/optimize-your-javascript-app-by-using-service-workers/

javascript优化

相关文章:

【视觉SLAM14讲】ch3课后题答案

1.验证旋转矩阵是正交矩阵 感觉下面这篇博客写的不错 http://www.cnblogs.com/caster99/p/4703033.html 总结一下:旋转矩阵是一个完美的矩阵——正交矩阵。①行列式为1,②每个列向量都是单位向量且相互正交,③它的逆等于它的转置。 2.罗德里…

【转载】邻接表表示法

图的邻接表表示法 图的邻接表表示法类似于树的孩子链表表示法。对于图G中的每个顶点v i ,该方法把所有邻接于v i 的顶点v j 链成一个带头 结点的单链表,这个单链表就称为顶点v i 的邻接表(Adjacency List)。 1. 邻接表的结点结构 &#xff08…

宝塔的服务忽然挂掉解决方法

先登录宝塔看内存是否满了 如果满了就点击文件,找到大文件进行删除,然后清空回收站,重启服务器,就解决了。 清空回收站:点击首页,打开终端,输入下面命令, 清空回收站的命令是&#…

免费创办网站_足够好的工程来创办一家互联网公司

免费创办网站I gave a guest lecture in an undergraduate software engineering class (CSCE431) at Texas A&M University in March 2019. Now I’ve turned this lecture into a blog post here, and hopefully some people on the Internet will find this useful.我于…

centos7下安装docker(11容器操作总结)

这段时间主要是学习了对容器的操作,包括:容器的状态:start,stop,restart,rename,pause,unpause,rm,attach,exec,kill,logs…

Js插入元素到数组的头部和尾部 unshift push

我们经常会使用JS 数组插入数据,下面看一下常用的 1. 在数组头部插入元素 var arr [1,2,3]; arr.unshift(0);arr 输出结果: //arr [0,1,2,3] 2. 在数组尾部插入元素 var arr [1,2,3]; arr.push(4);arr 输出结果: //arr [1,2,3,4]

测试用例设计白皮书--正交实验设计方法

一.方法简介利用因果图来设计测试用例时, 作为输入条件的原因与输出结果之间的因果关系,有时很难从软件需求规格说明中得到。往往因果关系非常庞大,以至于据此因果图而得到的测试用例数目多的惊人,给软件测试带来沉重的负担,为了有效地,合理地减少测试的…

ios单应用模式_如何为iOS 13暗模式设置应用

ios单应用模式Apple launched the much-awaited iOS 13 updates globally on September 19 across all iPhones launched within the past 4 years (back to the iPhone 6s). 苹果于9月19日在全球发布了期待已久的iOS 13更新,该更新适用于过去4年内发布的所有iPhone…

BZOJ1965 [Ahoi2005]SHUFFLE 洗牌 快速幂

欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1965 题意概括 对于扑克牌的一次洗牌是这样定义的,将一叠N(N为偶数)张扑克牌平均分成上下两叠,取下面一叠的第一张作为新的一叠的第一张&#xff0c…

uniapp 长链接 socket 封装

App.vue <script>import socket from @/util/IM.jsexport default {watch: {$route: function() {var page = getCurrentPages();console.log(watch-监听路由, page);}},globalData: {ImAuth: socket.connect(),},onLaunch: function(e) {},onShow: function() {socket.…

react项目开发步骤_成为专业React开发人员的31个步骤

react项目开发步骤我为达到可雇用水平而进行的每个项目和课程。 (Every single project and course I took to reach a hireable level.) Before I learned how to code, I used to ask developers how much time it took them to learn their craft — and how they managed t…

P1979 [NOIP]华容道

【问题描述】 小 B 最近迷上了华容道&#xff0c;可是他总是要花很长的时间才能完成一次。于是&#xff0c;他想到用编程来完成华容道&#xff1a;给定一种局面&#xff0c; 华容道是否根本就无法完成&#xff0c;如果能完成&#xff0c; 最少需要多少时间。 小 B 玩的华容道与…

JS计算两个时间相差多久,相差年,月,日,小时,分钟

计算一个时间戳距离当前的时间&#xff0c;例如&#xff1a; 几年前&#xff0c;几个月前&#xff0c;几天前&#xff0c;几小时前&#xff0c;几分钟前&#xff0c;刚刚。 输出效果 代码&#xff1a; function getDistanceDay(time) {let stime new Date().getTime();let u…

replace 使用函数作为第二参数

var sToChange “The sky is red.”;var reRed /red/;var sResultText sToChange.replace(reRed, function(sMatch) { return “blue”;}); sMatch 指的是被匹配到到的对象&#xff0c; return 返回替换的对象 var reBadWords /badword|anotherbadword/gi;var sU…

如何在Visual Studio Code中编译C ++代码

PS: This was published on my Blog here. PS&#xff1a;这已发布在我的Blog 此处 。 C is a statically-typed, free-form, (usually) compiled, multi-paradigm, intermediate-level general-purpose middle-level programming language.C 是一种静态类型的&#xff0c;自由…

敏捷冲刺每日报告四(Java-Team)

第四天报告&#xff08;10.28 周六&#xff09; 团队&#xff1a;Java-Team 成员&#xff1a; 章辉宇&#xff08;284&#xff09; 吴政楠&#xff08;286&#xff09; 陈阳&#xff08;PM&#xff1a;288&#xff09; 韩华颂&#xff08;142&#xff09; 胡志权&#xff08;1…

终止forEach的循环

上代码&#xff1a; let list[1,2,3] try {list.forEach(item > {if (item1) {console.log(等于1就跳出循环)throw new Error("EndIterative");}}) } catch (e) {}

大学可以学前端开发_所有开发人员在大学中应该学习的东西

大学可以学前端开发忘记“代码行” (Forget About "Lines of Code") Source资源 As a developer, youll hear a lot of crazy, unbelievable theories about what "lines of code" signify. Believe none of them. Lines of code is a ridiculous metric …

20162325 金立清 S2 W8 C17

20162325 2017-2018-2 《程序设计与数据结构》第8周学习总结 教材学习内容概要 二叉查找树是一棵二叉树&#xff0c;对于其中的每个结点&#xff0c;左子树上的元素小于父结点的值&#xff0c;而右子树上的元素大于等于父结点的值。 最有效的二叉树是平衡的&#xff0c;所以每次…

H5画布不显示图片的问题解决

在onReady 执行 <template><view class""><canvas style"" canvas-id"myCanvas" id"myCanvas"></canvas><!-- <view class"container"><img :src"tempFilePath" /></…

javascript十六进制数字和ASCII字符之间转换

var hex"0x29";//十六进制 var charValue String.fromCharCode(hex);//生成Unicode字符 var charCode charValue.charCodeAt(0);//获取指定字符的十进制表示. var hexOri"0x"charCode.toString(16);;//将int值转换为十六进制 alert("hex:&q…

html5编写网页代码_freeCodeCamp.org的未来-从向世界传授语言到编写代码的5年经验...

html5编写网页代码freeCodeCamp went live in October 2014. In the five years since, weve done quite a bit.freeCodeCamp于2014年10月上线。在此之后的五年中&#xff0c;我们做了很多工作。 In this article, well explore:在本文中&#xff0c;我们将探讨&#xff1a; …

程序员眼中的英文单词是这样的

来源&#xff1a;Jackie Han英语中一个单词可能有很多不同的意思。很多中国开发者外语本来就不好&#xff0c;概念是往往先入为主。甚至在不清楚一般意义的情况下&#xff0c;先记住了特定环境中的意思。 转载于:https://www.cnblogs.com/agileai/p/5166982.html

linux系统无法挂载U盘

插上U盘 [ 2407.650440] usb 1-3.3: new high speed USB device number 7 using s5p-ehci [ 2407.887332] usb 1-3.3: New USB device found, idVendor0951, idProduct1666, bcdDevice0100 [ 2407.894249] usb 1-3.3: New USB device strings: Mfr1, Product2, SerialNumber3 […

小程序输入框上推页面不上推

样式问题&#xff0c;把样式去掉就行

面试:你了解中兴吗_HTTP简介:您需要了解的所有内容

面试:你了解中兴吗In this article, I will walk you through how the world wide web works at a fundamental level.在本文中&#xff0c;我将向您介绍基本的万维网工作原理。 The core technology is HTTP - Hypertext Transfer Protocol. Its the communication protocol …

img-responsive class图片响应式

在BootStrap中&#xff0c;给<img>添加 .img-responsive样式就可以实现图片响应式。1<img src"..." class"img-responsive">转载于:https://www.cnblogs.com/zouyun/p/7761393.html

小程序开发卡券

前期准备 小程序内领取卡券 1.开发者须有一个有卡券权限的公众号&#xff08;服务号&#xff09;和认证后的小程序账号&#xff1b; 2.开发者须申请一个开放平台账号&#xff0c;并将小程序和公众号绑定在同一个开放平台账号下&#xff0c;关于开放平台的介绍请参照&#xff1…

php学习之道:WSDL具体解释(三)

通过声明方式定义绑定&#xff08;binding&#xff09;属性 假设你在服务中採用SOAP binding。你能够使用JAX-WS来指定一定数量的属性binding。这些属性指定相应你在WSDL中指定的属性。某些设置。比方參数类型&#xff0c;能够约束你实现的方法。这些设置也影响声明的效用。 SO…

什么是棉绒,它如何节省您的时间?

One of the biggest challenges in software development is time. It’s something we can’t easily get more of, but linting can help us make the most out of the time we have.时间是软件开发中最大的挑战之一。 这是我们无法轻易获得的更多东西&#xff0c;但是棉绒可…