首页 文章 精选 留言 我的

精选列表

搜索[javascript引擎],共10000篇文章
优秀的个人博客,低调大师

共筑Spark大数据引擎的七大工具

Spark正在数据处理领域卷起一场风暴。让我们通过本篇文章,看看为Spark的大数据平台起到推波助澜的几个重要工具。 Spark生态系统众生相 Apache Spark不仅仅让大数据处理起来更快,还让大数据处理起来更简单、功能更强大、更方便。Spark并非只是一项技术,它结合了诸多部分,新的功能和性能改进不断添加进来,每个部分都在不断完善之中。 本文介绍了Spark生态系统的每个主要部分:每个部分的功能,为什么很重要,是如何发展的,在哪方面不尽如人意,以及可能会往哪个方向发展。 Spark Core Spark的核心是恰如其名的Spark Core。除了协调和调度作业外,Spark Core还为Spark中的数据处理提供了基本的抽象机制,名为弹性分布式数据集(RDD)。 RDD对数据执行两个动作:转换和操作。前者转换数据,并将它们作为刚创新的RDD来提供;后者根据现有的RDD(比如对象数量)来计算结果。 Spark的速度很快,原因是转换和操作都保存在内存中。操作慢腾腾地评估,这意味着只有需要相关的数据时,才执行操作;然而,很难搞清楚什么在缓慢运行。 Spark的速度在不断提高。Java的内存管理往往给Spark带来问题,于是Project Tungsten计划避开JVM的内存和垃圾收集子系统,以此提高内存效率。 Spark API Spark主要是用Scala编写的,所以Spark的主要API长期以来也支持Scala。不过另外三种使用广泛得多的语言同样得到支持:Java(Spark也依赖它)、Python和R. 总的来说,你最好选择自己最擅长的那种语言,因为你需要的功能特性很可能在该语言中直接得到支持。只有一个例外:相比之下,SparkR中对机器学习的支持不大给力,目前只有一小批算法可供使用。不过将来这种情况势必会发生变化。 Spark SQL 千万不要低估了能够对批量数据执行SQL查询的能力或便利。Spark SQL提供了对Spark提供的数据执行SQL查询(并且请求列式DataFrame)的一种通用机制,包括通过ODBC/JDBC连接件进行管道处理的查询。你甚至不需要正规的数据源。Spark 1.6中添加了这一功能:支持以一种得到支持的格式查询扁平文件,就像Apache Drill那样。 Spark SQL其实并不用于更新数据,因为那与Spark的整个意义相悖。可以将因而生成的数据写回成新的Spark数据源(比如新的Parquet表),但是UPDATE查询并不得到支持。别指望诸如此类的功能特性很快就会推出;着眼于Spark SQL的改进大多数用于提升其性能,因为它也成了Spark Streaming的基础。 Spark Streaming Spark的设计让它得以支持许多处理方法,包括流处理――Spark Streaming因此得名。关于Spark Steaming的传统观点是,它还半生不熟,这意味着只有你不需要瞬间延迟,或者如果你还没有投入到另一种流数据处理解决方案(比如说Apache Storm),你才会使用它。 但是Storm在逐渐失去人气;长期使用Storm的推特此后已改用了自己的项目Heron。此外,Spark 2.0承诺会推出一种新的“结构化数据流”模式,以便对实时数据进行交互式Spark SQL查询,包括使用Spark 的机器学习库。至于其性能是否高得足以击败竞争对手仍需拭目以待,不过它值得认真考虑。 MLlib(机器学习) 机器学习技术素有既神奇,又困难之称。Spark让你可以对Spark中的数据运行许多常见的机器学习算法,从而使这些类型的分析容易得多,也更容易被Spark用户所使用。 MLlib中的可用算法数量众多,该框架每推出一个修订版,就会随之增多。话虽如此,一些类型的算法还是没有――比如说,涉及深度学习的任何算法。第三方正在利用Spark的人气来填补这一空白;比如说,雅虎可以借助CaffeOnSpark执行深度学习,它通过Spark充分利用了Caffe深度学习系统。 GraphX (图形计算) 描绘数百万实体之间的关系通常需要图形,这种数据构件描述了那些实体之间的相互关系。Spark的GraphX API让你可以使用Spark的一套方法,对数据执行图形操作,于是构建和转换这类图形的繁重任务卸载到了Spark。GraphX 还包括用于处理数据的几种常见算法,比如PageRank或标签传播(label propagation)。 从目前来看,GraphX的 一个主要限制是,它最适合静态图形。处理添加了新顶点的图形会严重影响性能。此外,如果你已经在使用一种成熟的图形数据库解决方案,GraphX还 不太可能取代它。 SparkR(Spark上的R) R语言为进行统计数值分析和机器学习工作提供了一种环境。Spark在2015年6月添加了支持R的功能,以匹配其支持Python和Scala的功能。 除了为潜在的Spark开发人员多提供一种语言外,SparkR还让R程序员们可以做之前做不了的许多事情,比如访问超过单一机器的内存容量的数据集,或者同时轻松地使用多个进程或在多个机器上运行分析。 SparkR还让R程序员可以充分利用Spark中的MLlib机器学习模块,创建一般的线性模型。遗憾的是,并非所有的MLlib功能在SparkR中得到支持,不过Spark每推出一个后续的修订版,都在填补R支持方面的差距。 本文转自d1net(转载)

优秀的个人博客,低调大师

JavaScript 中 async/await 的错误处理:为什么 try/catch 有时抓不到异常

在 Node.js 或浏览器里用 async/await 写异步逻辑时,很多人习惯把 await 包在 try/catch 里,但实际运行中总会遇到 catch 没生效、进程直接挂掉、或者 Promise 被 reject 后静默丢失的情况。这类问题通常出现在并发请求、定时任务、事件回调、以及忘记 await 的异步函数调用中。async 函数返回的 Promise 如果无人处理 rejection,就会触发 unhandledRejection;而在循环里对多个 Promise 使用 forEach 而不 await,也会让 try/catch 完全失去作用。这篇文章从运行时机制出发,拆解 try/catch 与 Promise 链的关系,并结合真实 Node 服务中的代码示例,说明如何正确捕获异步错误、如何避免未处理的 rejection,以及在不同场景下选择合适的错误传递方式。

优秀的个人博客,低调大师

JavaScript 中 Promise 链式调用里 then 里返回 Promise 的真实行为与常见误用

写业务代码时经常在一个 then 回调里再发起一个异步请求,比如拿到用户 ID 后查订单列表,很多人会顺手写 return fetch(...) 或者 return somePromise,但也会有人写成 somePromise.then(...) 却忘了 return。这两种写法在 Promise 链里的行为差别很大,前者会把内层 Promise 展开,让外层链等待内层完成,后者则让外层链直接拿到 undefined 继续往下走。更深一层的问题是,当 then 回调返回一个 thenable 对象时,V8 会走 PromiseResolve 流程去调用它的 then 方法,这个过程会额外消耗微任务,也容易被错误地认为“返回 Promise 就等于嵌套”。这篇文章从规范里的 Promise 解析过程出发,结合几个真实场景说明 then 回调返回值的处理规则,以及 async/await 混用 Promise 链时容易踩的坑。

优秀的个人博客,低调大师

JavaScript 中 at() 方法在数组和字符串中的实际应用与边界处理

在维护一个数据看板项目时,我经常需要从数组或字符串的末尾取元素。以前习惯用 `arr[arr.length - 1]` 或者 `str.charAt(str.length - 1)`,写多了总觉得啰嗦,而且一旦变量名很长,代码读起来就有点费劲。后来发现 ES2022 给数组、字符串和 TypedArray 都加了 `at()` 方法,可以直接传负数索引,从末尾往前数。这个 API 在 Chrome 92+、Node 16.6+ 都已经支持,但实际用的时候有几个细节容易踩坑:比如它不改变原数组,对稀疏数组的处理和普通索引一样,字符串上返回的是单个字符而不是码点。这篇文章会结合几个真实场景,聊聊 `at()` 的用法、和传统写法的对比,以及当索引越界或遇到非整数时它到底返回什么。 ## 从末尾取元素到底有多常见 假设有一个最近访问列表,按时间倒序排列,要拿最近一条记录: ```js const recentVisits = ['/home', '/dashboard', '/settings', '/profile']; // 老写法 const latest = recentVisits[recentVisits.length - 1]; // at() 写法 const latest = recentVisits.at(-1); ``` `at(-1)` 等价于 `length - 1`,但省掉了手动计算。对于 `at(-2)`、`at(-3)` 也一样直观。字符串上同样适用: ```js const fileName = 'report-2024-final.pdf'; const ext = fileName.at(-3) + fileName.at(-2) + fileName.at(-1); // 'pdf' ``` 当然截扩展名有更合适的 `split` 或 `slice`,这里只是展示负数索引在字符串上的表现。 ## 越界和非法参数时返回什么 `at()` 的行为和普通方括号索引在越界时一致:返回 `undefined`,不会抛错。 ```js const arr = [10, 20, 30]; console.log(arr.at(5)); // undefined console.log(arr.at(-5)); // undefined console.log(arr.at(1.5)); // 20?不,是 undefined ``` 最后一行值得注意:`at()` 会把参数先转成整数,但转换规则和 `Array.prototype.slice` 不同。它使用 `ToIntegerOrInfinity`,小数会直接截断。`1.5` 截断成 `1`,所以 `arr.at(1.5)` 实际返回 `20`。我在控制台验证过,确实如此。但如果你传 `NaN`,会被转成 `0`: ```js console.log(arr.at(NaN)); // 10 console.log(arr.at('1')); // 20,字符串 '1' 被转成数字 1 ``` 这些隐式转换在动态数据里容易出问题。比如从 URL query 取到的索引是字符串,直接传给 `at()` 反而能工作,但可读性差,最好显式 `Number()` 一下。 ## 稀疏数组和 at() 的互动 稀疏数组里空槽的行为和普通索引一致: ```js const sparse = [1, , 3]; console.log(sparse.at(1)); // undefined console.log(sparse.at(-2)); // undefined ``` 它不会像 `map` 或 `forEach` 那样跳过空槽,而是直接返回 `undefined`。如果你用 `at()` 来取稀疏数组末尾,得到的就是那个空槽的值。 ## 字符串上的 at() 和 Unicode `String.prototype.at()` 返回的是指定位置的 UTF-16 码元,不是完整码点。对于 emoji 或某些汉字(如 `𠮷`),一个字符占两个码元,`at()` 只取其中一个会得到乱码的一半: ```js const emoji = '😀'; console.log(emoji.at(0)); // '\uD83D' 半个代理对 console.log(emoji.at(1)); // '\uDE00' ``` 要按码点取字符,还是得用 `Array.from(str)` 或者展开运算符。所以 `at()` 适合处理普通 ASCII 或 BMP 内的字符,涉及 emoji 时得留个心眼。 ## 在 React 列表渲染中的一个小优化 之前写一个消息列表,需要取最后一条消息的 id 作为 key 的参考: ```jsx function MessageList({ messages }) { const lastId = messages.at(-1)?.id; return ( {messages.map(msg => ( {msg.text} ))} ); } ``` 这里用了可选链,因为 `messages` 可能为空数组,`at(-1)` 返回 `undefined`,再访问 `.id` 会报错。相比 `messages[messages.length - 1]?.id`,`at()` 写起来更短,意图也更明确。 ## Node.js 中的兼容性处理 如果项目需要支持 Node 14 或更早版本,`at()` 不存在。可以在入口文件加一个轻量 polyfill: ```js if (!Array.prototype.at) { Array.prototype.at = function(index) { const len = this.length; const relativeIndex = Math.trunc(index) || 0; const k = relativeIndex >= 0 ? relativeIndex : len + relativeIndex; if (k = len) return undefined; return this[k]; }; } ``` 字符串的 polyfill 类似,但要注意不要直接修改 `String.prototype`,除非你清楚影响范围。更稳妥的做法是用工具函数: ```js function charAt(str, index) { const i = index 推荐查看天气:https://www.0dtq.com/liangzihu/40tian.html 推荐查看天气:https://www.0dtq.com/echeng/ 推荐查看天气:https://www.0dtq.com/echeng/zuotian.html 推荐查看天气:https://www.0dtq.com/echeng/7tian.html 推荐查看天气:https://www.0dtq.com/echeng/10tian.html 推荐查看天气:https://www.0dtq.com/echeng/15tian.html 推荐查看天气:https://www.0dtq.com/echeng/40tian.html 推荐查看天气:https://www.0dtq.com/jingmen/ 推荐查看天气:https://www.0dtq.com/jingmen/zuotian.html 推荐查看天气:https://www.0dtq.com/jingmen/7tian.html 推荐查看天气:https://www.0dtq.com/jingmen/10tian.html 推荐查看天气:https://www.0dtq.com/jingmen/15tian.html 推荐查看天气:https://www.0dtq.com/jingmen/40tian.html 推荐查看天气:https://www.0dtq.com/dongbao/ 推荐查看天气:https://www.0dtq.com/dongbao/zuotian.html 推荐查看天气:https://www.0dtq.com/dongbao/7tian.html 推荐查看天气:https://www.0dtq.com/dongbao/10tian.html 推荐查看天气:https://www.0dtq.com/dongbao/15tian.html 推荐查看天气:https://www.0dtq.com/dongbao/40tian.html 推荐查看天气:https://www.0dtq.com/duodao/ 推荐查看天气:https://www.0dtq.com/duodao/zuotian.html 推荐查看天气:https://www.0dtq.com/duodao/7tian.html 推荐查看天气:https://www.0dtq.com/duodao/10tian.html 推荐查看天气:https://www.0dtq.com/duodao/15tian.html 推荐查看天气:https://www.0dtq.com/duodao/40tian.html 推荐查看天气:https://www.0dtq.com/jingshan/ 推荐查看天气:https://www.0dtq.com/jingshan/zuotian.html 推荐查看天气:https://www.0dtq.com/jingshan/7tian.html 推荐查看天气:https://www.0dtq.com/jingshan/10tian.html 推荐查看天气:https://www.0dtq.com/jingshan/15tian.html 推荐查看天气:https://www.0dtq.com/jingshan/40tian.html 推荐查看天气:https://www.0dtq.com/shayang/ 推荐查看天气:https://www.0dtq.com/shayang/zuotian.html 推荐查看天气:https://www.0dtq.com/shayang/7tian.html 推荐查看天气:https://www.0dtq.com/shayang/10tian.html 推荐查看天气:https://www.0dtq.com/shayang/15tian.html 推荐查看天气:https://www.0dtq.com/shayang/40tian.html 推荐查看天气:https://www.0dtq.com/zhongxiang/ 推荐查看天气:https://www.0dtq.com/zhongxiang/zuotian.html 推荐查看天气:https://www.0dtq.com/zhongxiang/7tian.html 推荐查看天气:https://www.0dtq.com/zhongxiang/10tian.html 推荐查看天气:https://www.0dtq.com/zhongxiang/15tian.html 推荐查看天气:https://www.0dtq.com/zhongxiang/40tian.html 推荐查看天气:https://www.0dtq.com/xiaogan/ 推荐查看天气:https://www.0dtq.com/xiaogan/zuotian.html 推荐查看天气:https://www.0dtq.com/xiaogan/7tian.html 推荐查看天气:https://www.0dtq.com/xiaogan/10tian.html 推荐查看天气:https://www.0dtq.com/xiaogan/15tian.html 推荐查看天气:https://www.0dtq.com/xiaogan/40tian.html 推荐查看天气:https://www.0dtq.com/xiaonan/ 推荐查看天气:https://www.0dtq.com/xiaonan/zuotian.html 推荐查看天气:https://www.0dtq.com/xiaonan/7tian.html 推荐查看天气:https://www.0dtq.com/xiaonan/10tian.html 推荐查看天气:https://www.0dtq.com/xiaonan/15tian.html 推荐查看天气:https://www.0dtq.com/xiaonan/40tian.html 推荐查看天气:https://www.0dtq.com/xiaochang/ 推荐查看天气:https://www.0dtq.com/xiaochang/zuotian.html 推荐查看天气:https://www.0dtq.com/xiaochang/7tian.html 推荐查看天气:https://www.0dtq.com/xiaochang/10tian.html 推荐查看天气:https://www.0dtq.com/xiaochang/15tian.html 推荐查看天气:https://www.0dtq.com/xiaochang/40tian.html 推荐查看天气:https://www.0dtq.com/dawu/ 推荐查看天气:https://www.0dtq.com/dawu/zuotian.html 推荐查看天气:https://www.0dtq.com/dawu/7tian.html 推荐查看天气:https://www.0dtq.com/dawu/10tian.html 推荐查看天气:https://www.0dtq.com/dawu/15tian.html 推荐查看天气:https://www.0dtq.com/dawu/40tian.html 推荐查看天气:https://www.0dtq.com/yunmeng/ 推荐查看天气:https://www.0dtq.com/yunmeng/zuotian.html 推荐查看天气:https://www.0dtq.com/yunmeng/7tian.html 推荐查看天气:https://www.0dtq.com/yunmeng/10tian.html 推荐查看天气:https://www.0dtq.com/yunmeng/15tian.html 推荐查看天气:https://www.0dtq.com/yunmeng/40tian.html 推荐查看天气:https://www.0dtq.com/anlu/ 推荐查看天气:https://www.0dtq.com/anlu/zuotian.html 推荐查看天气:https://www.0dtq.com/anlu/7tian.html 推荐查看天气:https://www.0dtq.com/anlu/10tian.html 推荐查看天气:https://www.0dtq.com/anlu/15tian.html 推荐查看天气:https://www.0dtq.com/anlu/40tian.html 推荐查看天气:https://www.0dtq.com/hanchuan/ 推荐查看天气:https://www.0dtq.com/hanchuan/zuotian.html 推荐查看天气:https://www.0dtq.com/hanchuan/7tian.html 推荐查看天气:https://www.0dtq.com/hanchuan/10tian.html 推荐查看天气:https://www.0dtq.com/hanchuan/15tian.html 推荐查看天气:https://www.0dtq.com/hanchuan/40tian.html 推荐查看天气:https://www.0dtq.com/jingzhou/ 推荐查看天气:https://www.0dtq.com/jingzhou/zuotian.html 推荐查看天气:https://www.0dtq.com/jingzhou/7tian.html 推荐查看天气:https://www.0dtq.com/jingzhou/10tian.html 推荐查看天气:https://www.0dtq.com/jingzhou/15tian.html 推荐查看天气:https://www.0dtq.com/jingzhou/40tian.html 推荐查看天气:https://www.0dtq.com/shashi/ 推荐查看天气:https://www.0dtq.com/shashi/zuotian.html 推荐查看天气:https://www.0dtq.com/shashi/7tian.html 推荐查看天气:https://www.0dtq.com/shashi/10tian.html 推荐查看天气:https://www.0dtq.com/shashi/15tian.html 推荐查看天气:https://www.0dtq.com/shashi/40tian.html 推荐查看天气:https://www.0dtq.com/gongan/ 推荐查看天气:https://www.0dtq.com/gongan/zuotian.html 推荐查看天气:https://www.0dtq.com/gongan/7tian.html 推荐查看天气:https://www.0dtq.com/gongan/10tian.html 推荐查看天气:https://www.0dtq.com/gongan/15tian.html 推荐查看天气:https://www.0dtq.com/gongan/40tian.html 推荐查看天气:https://www.0dtq.com/jianli/ 推荐查看天气:https://www.0dtq.com/jianli/zuotian.html 推荐查看天气:https://www.0dtq.com/jianli/7tian.html 推荐查看天气:https://www.0dtq.com/jianli/10tian.html 推荐查看天气:https://www.0dtq.com/jianli/15tian.html 推荐查看天气:https://www.0dtq.com/jianli/40tian.html 推荐查看天气:https://www.0dtq.com/jiangling/ 推荐查看天气:https://www.0dtq.com/jiangling/zuotian.html 推荐查看天气:https://www.0dtq.com/jiangling/7tian.html 推荐查看天气:https://www.0dtq.com/jiangling/10tian.html 推荐查看天气:https://www.0dtq.com/jiangling/15tian.html 推荐查看天气:https://www.0dtq.com/jiangling/40tian.html 推荐查看天气:https://www.0dtq.com/shishou/ 推荐查看天气:https://www.0dtq.com/shishou/zuotian.html 推荐查看天气:https://www.0dtq.com/shishou/7tian.html 推荐查看天气:https://www.0dtq.com/shishou/10tian.html 推荐查看天气:https://www.0dtq.com/shishou/15tian.html 推荐查看天气:https://www.0dtq.com/shishou/40tian.html 推荐查看天气:https://www.0dtq.com/honghu/ 推荐查看天气:https://www.0dtq.com/honghu/zuotian.html 推荐查看天气:https://www.0dtq.com/honghu/7tian.html 推荐查看天气:https://www.0dtq.com/honghu/10tian.html 推荐查看天气:https://www.0dtq.com/honghu/15tian.html 推荐查看天气:https://www.0dtq.com/honghu/40tian.html 推荐查看天气:https://www.0dtq.com/songzi/ 推荐查看天气:https://www.0dtq.com/songzi/zuotian.html 推荐查看天气:https://www.0dtq.com/songzi/7tian.html 推荐查看天气:https://www.0dtq.com/songzi/10tian.html 推荐查看天气:https://www.0dtq.com/songzi/15tian.html 推荐查看天气:https://www.0dtq.com/songzi/40tian.html 推荐查看天气:https://www.0dtq.com/huanggang/ 推荐查看天气:https://www.0dtq.com/huanggang/zuotian.html 推荐查看天气:https://www.0dtq.com/huanggang/7tian.html 推荐查看天气:https://www.0dtq.com/huanggang/10tian.html 推荐查看天气:https://www.0dtq.com/huanggang/15tian.html 推荐查看天气:https://www.0dtq.com/huanggang/40tian.html 推荐查看天气:https://www.0dtq.com/huangzhou/ 推荐查看天气:https://www.0dtq.com/huangzhou/zuotian.html 推荐查看天气:https://www.0dtq.com/huangzhou/7tian.html 推荐查看天气:https://www.0dtq.com/huangzhou/10tian.html 推荐查看天气:https://www.0dtq.com/huangzhou/15tian.html 推荐查看天气:https://www.0dtq.com/huangzhou/40tian.html 推荐查看天气:https://www.0dtq.com/tuanfeng/ 推荐查看天气:https://www.0dtq.com/tuanfeng/zuotian.html 推荐查看天气:https://www.0dtq.com/tuanfeng/7tian.html 推荐查看天气:https://www.0dtq.com/tuanfeng/10tian.html 推荐查看天气:https://www.0dtq.com/tuanfeng/15tian.html 推荐查看天气:https://www.0dtq.com/tuanfeng/40tian.html 推荐查看天气:https://www.0dtq.com/hongan/ 推荐查看天气:https://www.0dtq.com/hongan/zuotian.html 推荐查看天气:https://www.0dtq.com/hongan/7tian.html 推荐查看天气:https://www.0dtq.com/hongan/10tian.html 推荐查看天气:https://www.0dtq.com/hongan/15tian.html 推荐查看天气:https://www.0dtq.com/hongan/40tian.html 推荐查看天气:https://www.0dtq.com/luotian/ 推荐查看天气:https://www.0dtq.com/luotian/zuotian.html 推荐查看天气:https://www.0dtq.com/luotian/7tian.html 推荐查看天气:https://www.0dtq.com/luotian/10tian.html 推荐查看天气:https://www.0dtq.com/luotian/15tian.html 推荐查看天气:https://www.0dtq.com/luotian/40tian.html 推荐查看天气:https://www.0dtq.com/huanggangyingshan/ 推荐查看天气:https://www.0dtq.com/huanggangyingshan/zuotian.html 推荐查看天气:https://www.0dtq.com/huanggangyingshan/7tian.html 推荐查看天气:https://www.0dtq.com/huanggangyingshan/10tian.html 推荐查看天气:https://www.0dtq.com/huanggangyingshan/15tian.html 推荐查看天气:https://www.0dtq.com/huanggangyingshan/40tian.html 推荐查看天气:https://www.0dtq.com/huanggangxishui/ 推荐查看天气:https://www.0dtq.com/huanggangxishui/zuotian.html 推荐查看天气:https://www.0dtq.com/huanggangxishui/7tian.html 推荐查看天气:https://www.0dtq.com/huanggangxishui/10tian.html 推荐查看天气:https://www.0dtq.com/huanggangxishui/15tian.html 推荐查看天气:https://www.0dtq.com/huanggangxishui/40tian.html 推荐查看天气:https://www.0dtq.com/qinchun/ 推荐查看天气:https://www.0dtq.com/qinchun/zuotian.html 推荐查看天气:https://www.0dtq.com/qinchun/7tian.html 推荐查看天气:https://www.0dtq.com/qinchun/10tian.html 推荐查看天气:https://www.0dtq.com/qinchun/15tian.html 推荐查看天气:https://www.0dtq.com/qinchun/40tian.html 推荐查看天气:https://www.0dtq.com/huangmei/ 推荐查看天气:https://www.0dtq.com/huangmei/zuotian.html 推荐查看天气:https://www.0dtq.com/huangmei/7tian.html 推荐查看天气:https://www.0dtq.com/huangmei/10tian.html 推荐查看天气:https://www.0dtq.com/huangmei/15tian.html 推荐查看天气:https://www.0dtq.com/huangmei/40tian.html 推荐查看天气:https://www.0dtq.com/wuxue/ 推荐查看天气:https://www.0dtq.com/wuxue/zuotian.html 推荐查看天气:https://www.0dtq.com/wuxue/7tian.html 推荐查看天气:https://www.0dtq.com/wuxue/10tian.html 推荐查看天气:https://www.0dtq.com/wuxue/15tian.html 推荐查看天气:https://www.0dtq.com/wuxue/40tian.html 推荐查看天气:https://www.0dtq.com/xianning/ 推荐查看天气:https://www.0dtq.com/xianning/zuotian.html 推荐查看天气:https://www.0dtq.com/xianning/7tian.html 推荐查看天气:https://www.0dtq.com/xianning/10tian.html 推荐查看天气:https://www.0dtq.com/xianning/15tian.html 推荐查看天气:https://www.0dtq.com/xianning/40tian.html 推荐查看天气:https://www.0dtq.com/xianan/

优秀的个人博客,低调大师

JavaScript 中 requestAnimationFrame 在后台标签页不触发的问题排查与替代方案

做数据大屏或者定时轮询渲染的时候,很容易遇到一个现象:页面切到后台再切回来,某些依赖于 requestAnimationFrame 的动画或状态更新会「卡住」几秒,甚至像是什么都没发生。浏览器为了省电和降低 CPU,在标签页不可见时会暂停 requestAnimationFrame 的回调,把它降频甚至完全冻结。但业务里可能用它来驱动非纯视觉任务,比如轮询状态、计算布局、播放帧序列,这时候就出问题了。下面从一次实际排查过程说起,看看为什么会这样,以及除了切回页面强制刷新外,还有哪些更可控的写法。 ## 问题复现 假设有一个组件用 rAF 来做一个平滑的数字滚动,同时每帧检查一下远程任务是否完成。 ```js function startLoop() { let last = performance.now(); function frame(now) { const delta = now - last; if (delta > 16) { renderNumber(); checkTaskStatus(); last = now; } requestAnimationFrame(frame); } requestAnimationFrame(frame); } ``` 在浏览器里正常切换标签页后,`checkTaskStatus` 不会执行,等回来时又恢复正常。如果任务完成依赖这个轮询,用户切走再回来可能发现状态还停在旧数据上。 ## 为什么后台标签页没回调 规范里没有规定 rAF 在页面不可见时应该继续以 60fps 运行。实际情况是,大多数浏览器在文档进入 hidden 状态后,会把 rAF 回调挂起,或者降低到非常低的频率(比如 1fps 甚至 0)。因为 rAF 的本意是配合屏幕刷新,不可见的页面没有刷新需求,继续运行等于白白耗电。 可以用 `document.visibilityState` 验证: ```js let count = 0; function tick() { count++; console.log('frame', count, document.visibilityState); requestAnimationFrame(tick); } requestAnimationFrame(tick); ``` 在后台标签页停几秒再切回来,控制台里帧数几乎不增长。这不是 bug,是节能策略。 ## 如果只是做动画,应该怎么做 纯视觉动画在页面不可见时暂停是合理的,用户看不到,继续跑也没意义。如果希望切回来时从当前状态继续,而不是「时间跳变」,可以用时间戳计算差值,而不是依赖帧数累加。 ```js let startTime = null; function animate(now) { if (startTime === null) startTime = now; const elapsed = now - startTime; const progress = Math.min(elapsed / 1000, 1); element.style.transform = `translateX(${progress * 200}px)`; if (progress { checkTaskStatus(); }, 1000); } function stopPolling() { if (timer) clearInterval(timer); timer = null; } ``` 如果任务对时效性要求更高,可以在 `visibilitychange` 里做补偿:切回页面时立即执行一次。 ```js document.addEventListener('visibilitychange', () => { if (document.visibilityState === 'visible') { checkTaskStatus(); } }); ``` ## 后台仍然需要执行的场景 有些逻辑必须持续运行,比如 WebSocket 心跳、音频处理、Web Worker 里的计算。这些不应该依赖主线程的 rAF。常见做法是把逻辑放进 Worker,Worker 的定时器不会被页面可见性冻结(但可能被浏览器整体节流策略影响,至少不受 visibility 直接控制)。 ```js // worker.js setInterval(() => { self.postMessage('heartbeat'); }, 5000); // main.js const worker = new Worker('worker.js'); worker.onmessage = (e) => { if (e.data === 'heartbeat') { sendHeartbeat(); } }; ``` WebSocket 心跳也可以直接用 `setInterval` 在页面里发,切后台时浏览器可能降低频率,但多数情况下不会完全停止。真正完全停止的情况出现在移动端切到桌面或锁屏,这时只能靠 Service Worker 或原生推送补足。 ## 兼容旧代码的折中处理 如果项目里已经大量使用了 rAF 做非视觉逻辑,全部重写成本高。可以在封装层加一个开关,在页面隐藏时改用 `setInterval` 兜底,可见时再切回 rAF。 ```js class FrameLoop { constructor(callback, fallbackInterval = 1000) { this.callback = callback; this.fallbackInterval = fallbackInterval; this.rafId = null; this.timer = null; this.onVisibilityChange = this.onVisibilityChange.bind(this); document.addEventListener('visibilitychange', this.onVisibilityChange); this.start(); } start() { this.stop(); if (document.visibilityState === 'visible') { const loop = () => { this.callback(); this.rafId = requestAnimationFrame(loop); }; this.rafId = requestAnimationFrame(loop); } else { this.timer = setInterval(() => this.callback(), this.fallbackInterval); } } stop() { if (this.rafId) cancelAnimationFrame(this.rafId); if (this.timer) clearInterval(this.timer); this.rafId = null; this.timer = null; } onVisibilityChange() { this.start(); } destroy() { this.stop(); document.removeEventListener('visibilitychange', this.onVisibilityChange); } } ``` 这样既保留了可见时的帧同步,又保证后台不会完全停止响应。回调里要注意不要做太重的事情,因为兜底间隔可能只有 1 秒,堆积起来反而更卡。 理解 rAF 的节能行为之后,选型就清晰了:画面相关用它,定时任务用定时器,必须后台运行的放 Worker。切页面的卡顿大多不是代码写错,而是把两种不同目的的东西混在了一起。 推荐查看天气:http://www.362tq.com/wangcang/zuotian.html 推荐查看天气:http://www.362tq.com/wangcang/7tian.html 推荐查看天气:http://www.362tq.com/wangcang/10tian.html 推荐查看天气:http://www.362tq.com/wangcang/15tian.html 推荐查看天气:http://www.362tq.com/wangcang/40tian.html 推荐查看天气:http://www.362tq.com/qingchuan/ 推荐查看天气:http://www.362tq.com/qingchuan/zuotian.html 推荐查看天气:http://www.362tq.com/qingchuan/7tian.html 推荐查看天气:http://www.362tq.com/qingchuan/10tian.html 推荐查看天气:http://www.362tq.com/qingchuan/15tian.html 推荐查看天气:http://www.362tq.com/qingchuan/40tian.html 推荐查看天气:http://www.362tq.com/jiange/ 推荐查看天气:http://www.362tq.com/jiange/zuotian.html 推荐查看天气:http://www.362tq.com/jiange/7tian.html 推荐查看天气:http://www.362tq.com/jiange/10tian.html 推荐查看天气:http://www.362tq.com/jiange/15tian.html 推荐查看天气:http://www.362tq.com/jiange/40tian.html 推荐查看天气:http://www.362tq.com/cangxi/ 推荐查看天气:http://www.362tq.com/cangxi/zuotian.html 推荐查看天气:http://www.362tq.com/cangxi/7tian.html 推荐查看天气:http://www.362tq.com/cangxi/10tian.html 推荐查看天气:http://www.362tq.com/cangxi/15tian.html 推荐查看天气:http://www.362tq.com/cangxi/40tian.html 推荐查看天气:http://www.362tq.com/suining/ 推荐查看天气:http://www.362tq.com/suining/zuotian.html 推荐查看天气:http://www.362tq.com/suining/7tian.html 推荐查看天气:http://www.362tq.com/suining/10tian.html 推荐查看天气:http://www.362tq.com/suining/15tian.html 推荐查看天气:http://www.362tq.com/suining/40tian.html 推荐查看天气:http://www.362tq.com/chuanshan/ 推荐查看天气:http://www.362tq.com/chuanshan/zuotian.html 推荐查看天气:http://www.362tq.com/chuanshan/7tian.html 推荐查看天气:http://www.362tq.com/chuanshan/10tian.html 推荐查看天气:http://www.362tq.com/chuanshan/15tian.html 推荐查看天气:http://www.362tq.com/chuanshan/40tian.html 推荐查看天气:http://www.362tq.com/anju/ 推荐查看天气:http://www.362tq.com/anju/zuotian.html 推荐查看天气:http://www.362tq.com/anju/7tian.html 推荐查看天气:http://www.362tq.com/anju/10tian.html 推荐查看天气:http://www.362tq.com/anju/15tian.html 推荐查看天气:http://www.362tq.com/anju/40tian.html 推荐查看天气:http://www.362tq.com/pengxi/ 推荐查看天气:http://www.362tq.com/pengxi/zuotian.html 推荐查看天气:http://www.362tq.com/pengxi/7tian.html 推荐查看天气:http://www.362tq.com/pengxi/10tian.html 推荐查看天气:http://www.362tq.com/pengxi/15tian.html 推荐查看天气:http://www.362tq.com/pengxi/40tian.html 推荐查看天气:http://www.362tq.com/shehong/ 推荐查看天气:http://www.362tq.com/shehong/zuotian.html 推荐查看天气:http://www.362tq.com/shehong/7tian.html 推荐查看天气:http://www.362tq.com/shehong/10tian.html 推荐查看天气:http://www.362tq.com/shehong/15tian.html 推荐查看天气:http://www.362tq.com/shehong/40tian.html 推荐查看天气:http://www.362tq.com/daying/ 推荐查看天气:http://www.362tq.com/daying/zuotian.html 推荐查看天气:http://www.362tq.com/daying/7tian.html 推荐查看天气:http://www.362tq.com/daying/10tian.html 推荐查看天气:http://www.362tq.com/daying/15tian.html 推荐查看天气:http://www.362tq.com/daying/40tian.html 推荐查看天气:http://www.362tq.com/neijiang/ 推荐查看天气:http://www.362tq.com/neijiang/zuotian.html 推荐查看天气:http://www.362tq.com/neijiang/7tian.html 推荐查看天气:http://www.362tq.com/neijiang/10tian.html 推荐查看天气:http://www.362tq.com/neijiang/15tian.html 推荐查看天气:http://www.362tq.com/neijiang/40tian.html 推荐查看天气:http://www.362tq.com/dongxing/ 推荐查看天气:http://www.362tq.com/dongxing/zuotian.html 推荐查看天气:http://www.362tq.com/dongxing/7tian.html 推荐查看天气:http://www.362tq.com/dongxing/10tian.html 推荐查看天气:http://www.362tq.com/dongxing/15tian.html 推荐查看天气:http://www.362tq.com/dongxing/40tian.html 推荐查看天气:http://www.362tq.com/najiangweiyuan/ 推荐查看天气:http://www.362tq.com/najiangweiyuan/zuotian.html 推荐查看天气:http://www.362tq.com/najiangweiyuan/7tian.html 推荐查看天气:http://www.362tq.com/najiangweiyuan/10tian.html 推荐查看天气:http://www.362tq.com/najiangweiyuan/15tian.html 推荐查看天气:http://www.362tq.com/najiangweiyuan/40tian.html 推荐查看天气:http://www.362tq.com/zizhong/ 推荐查看天气:http://www.362tq.com/zizhong/zuotian.html 推荐查看天气:http://www.362tq.com/zizhong/7tian.html 推荐查看天气:http://www.362tq.com/zizhong/10tian.html 推荐查看天气:http://www.362tq.com/zizhong/15tian.html 推荐查看天气:http://www.362tq.com/zizhong/40tian.html 推荐查看天气:http://www.362tq.com/longchang/ 推荐查看天气:http://www.362tq.com/longchang/zuotian.html 推荐查看天气:http://www.362tq.com/longchang/7tian.html 推荐查看天气:http://www.362tq.com/longchang/10tian.html 推荐查看天气:http://www.362tq.com/longchang/15tian.html 推荐查看天气:http://www.362tq.com/longchang/40tian.html 推荐查看天气:http://www.362tq.com/leshan/ 推荐查看天气:http://www.362tq.com/leshan/zuotian.html 推荐查看天气:http://www.362tq.com/leshan/7tian.html 推荐查看天气:http://www.362tq.com/leshan/10tian.html 推荐查看天气:http://www.362tq.com/leshan/15tian.html 推荐查看天气:http://www.362tq.com/leshan/40tian.html 推荐查看天气:http://www.362tq.com/shizhong/ 推荐查看天气:http://www.362tq.com/shizhong/zuotian.html 推荐查看天气:http://www.362tq.com/shizhong/7tian.html 推荐查看天气:http://www.362tq.com/shizhong/10tian.html 推荐查看天气:http://www.362tq.com/shizhong/15tian.html 推荐查看天气:http://www.362tq.com/shizhong/40tian.html 推荐查看天气:http://www.362tq.com/wutongqiao/ 推荐查看天气:http://www.362tq.com/wutongqiao/zuotian.html 推荐查看天气:http://www.362tq.com/wutongqiao/7tian.html 推荐查看天气:http://www.362tq.com/wutongqiao/10tian.html 推荐查看天气:http://www.362tq.com/wutongqiao/15tian.html 推荐查看天气:http://www.362tq.com/wutongqiao/40tian.html 推荐查看天气:http://www.362tq.com/jinkouhe/ 推荐查看天气:http://www.362tq.com/jinkouhe/zuotian.html 推荐查看天气:http://www.362tq.com/jinkouhe/7tian.html 推荐查看天气:http://www.362tq.com/jinkouhe/10tian.html 推荐查看天气:http://www.362tq.com/jinkouhe/15tian.html 推荐查看天气:http://www.362tq.com/jinkouhe/40tian.html 推荐查看天气:http://www.362tq.com/jianwei/ 推荐查看天气:http://www.362tq.com/jianwei/zuotian.html 推荐查看天气:http://www.362tq.com/jianwei/7tian.html 推荐查看天气:http://www.362tq.com/jianwei/10tian.html 推荐查看天气:http://www.362tq.com/jianwei/15tian.html 推荐查看天气:http://www.362tq.com/jianwei/40tian.html 推荐查看天气:http://www.362tq.com/jingyan/ 推荐查看天气:http://www.362tq.com/jingyan/zuotian.html 推荐查看天气:http://www.362tq.com/jingyan/7tian.html 推荐查看天气:http://www.362tq.com/jingyan/10tian.html 推荐查看天气:http://www.362tq.com/jingyan/15tian.html 推荐查看天气:http://www.362tq.com/jingyan/40tian.html 推荐查看天气:http://www.362tq.com/jiajiang/ 推荐查看天气:http://www.362tq.com/jiajiang/zuotian.html 推荐查看天气:http://www.362tq.com/jiajiang/7tian.html 推荐查看天气:http://www.362tq.com/jiajiang/10tian.html 推荐查看天气:http://www.362tq.com/jiajiang/15tian.html 推荐查看天气:http://www.362tq.com/jiajiang/40tian.html 推荐查看天气:http://www.362tq.com/muchuan/ 推荐查看天气:http://www.362tq.com/muchuan/zuotian.html 推荐查看天气:http://www.362tq.com/muchuan/7tian.html 推荐查看天气:http://www.362tq.com/muchuan/10tian.html 推荐查看天气:http://www.362tq.com/muchuan/15tian.html 推荐查看天气:http://www.362tq.com/muchuan/40tian.html 推荐查看天气:http://www.362tq.com/ebian/ 推荐查看天气:http://www.362tq.com/ebian/zuotian.html 推荐查看天气:http://www.362tq.com/ebian/7tian.html 推荐查看天气:http://www.362tq.com/ebian/10tian.html 推荐查看天气:http://www.362tq.com/ebian/15tian.html 推荐查看天气:http://www.362tq.com/ebian/40tian.html 推荐查看天气:http://www.362tq.com/mabian/ 推荐查看天气:http://www.362tq.com/mabian/zuotian.html 推荐查看天气:http://www.362tq.com/mabian/7tian.html 推荐查看天气:http://www.362tq.com/mabian/10tian.html 推荐查看天气:http://www.362tq.com/mabian/15tian.html 推荐查看天气:http://www.362tq.com/mabian/40tian.html 推荐查看天气:http://www.362tq.com/emeishan/ 推荐查看天气:http://www.362tq.com/emeishan/zuotian.html 推荐查看天气:http://www.362tq.com/emeishan/7tian.html 推荐查看天气:http://www.362tq.com/emeishan/10tian.html 推荐查看天气:http://www.362tq.com/emeishan/15tian.html 推荐查看天气:http://www.362tq.com/emeishan/40tian.html 推荐查看天气:http://www.362tq.com/nanchong/ 推荐查看天气:http://www.362tq.com/nanchong/zuotian.html 推荐查看天气:http://www.362tq.com/nanchong/7tian.html 推荐查看天气:http://www.362tq.com/nanchong/10tian.html 推荐查看天气:http://www.362tq.com/nanchong/15tian.html 推荐查看天气:http://www.362tq.com/nanchong/40tian.html 推荐查看天气:http://www.362tq.com/shunqing/ 推荐查看天气:http://www.362tq.com/shunqing/zuotian.html 推荐查看天气:http://www.362tq.com/shunqing/7tian.html 推荐查看天气:http://www.362tq.com/shunqing/10tian.html 推荐查看天气:http://www.362tq.com/shunqing/15tian.html 推荐查看天气:http://www.362tq.com/shunqing/40tian.html 推荐查看天气:http://www.362tq.com/jialing/ 推荐查看天气:http://www.362tq.com/jialing/zuotian.html 推荐查看天气:http://www.362tq.com/jialing/7tian.html 推荐查看天气:http://www.362tq.com/jialing/10tian.html 推荐查看天气:http://www.362tq.com/jialing/15tian.html 推荐查看天气:http://www.362tq.com/jialing/40tian.html 推荐查看天气:http://www.362tq.com/nanbu/ 推荐查看天气:http://www.362tq.com/nanbu/zuotian.html 推荐查看天气:http://www.362tq.com/nanbu/7tian.html 推荐查看天气:http://www.362tq.com/nanbu/10tian.html 推荐查看天气:http://www.362tq.com/nanbu/15tian.html 推荐查看天气:http://www.362tq.com/nanbu/40tian.html 推荐查看天气:http://www.362tq.com/yingshan/ 推荐查看天气:http://www.362tq.com/yingshan/zuotian.html 推荐查看天气:http://www.362tq.com/yingshan/7tian.html 推荐查看天气:http://www.362tq.com/yingshan/10tian.html 推荐查看天气:http://www.362tq.com/yingshan/15tian.html 推荐查看天气:http://www.362tq.com/yingshan/40tian.html 推荐查看天气:http://www.362tq.com/pengan/ 推荐查看天气:http://www.362tq.com/pengan/zuotian.html 推荐查看天气:http://www.362tq.com/pengan/7tian.html 推荐查看天气:http://www.362tq.com/pengan/10tian.html 推荐查看天气:http://www.362tq.com/pengan/15tian.html 推荐查看天气:http://www.362tq.com/pengan/40tian.html 推荐查看天气:http://www.362tq.com/yilong/ 推荐查看天气:http://www.362tq.com/yilong/zuotian.html 推荐查看天气:http://www.362tq.com/yilong/7tian.html 推荐查看天气:http://www.362tq.com/yilong/10tian.html 推荐查看天气:http://www.362tq.com/yilong/15tian.html 推荐查看天气:http://www.362tq.com/yilong/40tian.html 推荐查看天气:http://www.362tq.com/xichong/ 推荐查看天气:http://www.362tq.com/xichong/zuotian.html 推荐查看天气:http://www.362tq.com/xichong/7tian.html

优秀的个人博客,低调大师

JavaScript中关于this关键字的魔幻现实 一篇文章搞懂他

不得不承认JS中存在一些“历史的遗产”,比如基于全局变量的编程模型。 如果写一个函数,如: function test(name){ var namex = name console.log(this.namex) } test('jack') 方法调用之后,this.namex=undefined,为何呢?因为此处的this是全局对象window,这就令人比较头大。 不过别着急,慢慢往下看,关于this还有很多魔幻的现实,我们来一一解锁。 1、完整的函数调用 还是上面的代码,其实他的完整调用写法是这样的: function test(name){ var namex = name console.log(this.namex) } test.call(undefined, 'jack') test是一个函数对象--即Function对象,Function.prototype有call方法。call()会有两个参数,第一个参数是this上下文对象,第二个参数是函数入参列表。 如果call()传入的this上下文是undefined或null,那么window对象将成为默认的this上下文 2、对象中的this const obj = { name: 'Jack', greet: function() { console.log(this.name) } } obj.greet() //简写调用 obj.greet.call(obj) //完整调用 obj.greet()中的this无疑就是obj对象 3、对象方法中嵌套函数的this 对2中的代码进行修改: const obj = { name: 'Jack', greet: function() { retufn function(){console.log(this.name)} } } obj.greet()() //输出null 需要注意的是嵌套函数中的this依然是window,为什么呢?可以拆分来看: var greet = obj.greet() greet() // = greet.call(undefined) 嵌套函数被调用的时候,真实的调用者上下文是undefined,也就是window 4、原型与this function Clt() { } Clt.prototype.x = 10 Clt.prototype.test = function () { console.log(this) this.y = this.x + 1 } let bean = new Clt() bean.test(); console.log(bean.y) test方法中输出的this是一个Clt对象。 这里需要引入一个新的概念:构造器函数。 new关键字就是构造器函数,它的作用是: 一旦函数被new来调用,就会创建一个链接到该函数的prototype属性的新对象,同时this会被绑定到那个新对象上 理解了构造器函数,我们就理解了原型与this的关系了,因为new会重新指定this上下文 5、箭头函数与this ECMAScript6出现了箭头函数的用法,关于箭头函数中的this,需要先记住一句话: 函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象。 看代码(引自阮一峰老师的教程:https://es6.ruanyifeng.com/#docs/function#%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0): function foo() { setTimeout(() => { console.log('id:', this.id); }, 100); } var id = 21; foo.call({ id: 42 }); // id: 42 此处的this指向了foo的this上下文,即定义时的this对象。 箭头函数与非箭头函数的this区别: function Timer() { this.s1 = 0; this.s2 = 0; // 箭头函数 setInterval(() => this.s1++, 1000); // 普通函数 setInterval(function () { this.s2++; }, 1000); } var timer = new Timer(); setTimeout(() => console.log('s1: ', timer.s1), 3100); setTimeout(() => console.log('s2: ', timer.s2), 3100); // s1: 3 // s2: 0 箭头函数的this指向了定义时所在对象的this;非箭头函数的this指向了运行时的作用域,即全局域window this的指向固定化,也算是解决了一些历史旧账,无疑带来的很大好处,比如封装调用: var handler = { id: '123456', init: function() { document.addEventListener('click', event => this.doSomething(event.type), false); }, doSomething: function(type) { console.log('Handling ' + type + ' for ' + this.id); } }; 一个嵌套的例子: function foo() { return () => { return () => { return () => { console.log('id:', this.id); }; }; }; } var f = foo.call({id: 1}); var t1 = f.call({id: 2})()(); // id: 1 var t2 = f().call({id: 3})(); // id: 1 var t3 = f()().call({id: 4}); // id: 1 最里层的this也绑定到了定义时所在对象的—即foo的this 总结 ECMAScript6的箭头函数也算是解决了一些历史问题。不过正如HTML5离不开HTML4一样,因为只有保持了继承与兼容,新技术的推广才会更加的迅速。 享受语言新特性之余,也要搞清楚旧版本的一些细节,这有助于更全面的掌握知识。 希望这篇文章对您有用!

优秀的个人博客,低调大师

通过阶乘的例子,练习在JavaScript, Scala和ABAP里实现尾递归(Tail Recursion)

Before we start to research tail recursion, let’s first have a look at the normal recursion. A simple factorial implementation by recursion: function factorial(n){ if(n ===1) { return 1; } return n *factorial(n -1); } Let N = 5, see how new stack frame is created for each time of recursive call: We have two stack frames now, one stores the context when n = 5, and the topmost one for current calculation: n = 4 Now since n equals to 1, we stop recursion. The current stack frame ( n = 1 ) will be poped up, the frame under it will be activated and become the topmost frame, with calculated result 1 passed into. key note for normal recursion: during recursion, every generated stack frame is needed and could not e destroyed until the final result is calculated. The calculation is actually not started until the recursion reaches the end ( the condition n === 1 fulfills ). If N is a big integer, it will lead to huge number of stack frames and finally the “stack overflow” or “out of memory” is inevitable. tail recursion Source code below: function tailFactorial(n, total) { if(n ===1) return total; return tailFactorial(n -1, n * total); } function factorial2(n) { return tailFactorial(n,1); } There are two biggest differences compared with normal recursion: (1) A new internal function tailFactorial is introduced here. (2) The calculation is actually now spread within every recursive stack frame. Each frame finishes one part of calculation and pass the current result to the next frame. Once the current stack frame finishes its task, it is actually not needed any more. And thus for example the model browser can then do some optimization on those useless stack frames. Observe the stack frame for tail recursion step by step: stack popped up: When N = 20, the tail recursion has a far better performance than the normal recursion: Update 2016-01-11 Tail recursion implementation via Scala: The interesting thing is, after the Scala code is compiled into Java Byte code, compiler will eliminate the recursion automatically: Tail Recursion in ABAP First this is the normal recursion: REPORT zrecursion. START-OF-SELECTION. DATA: lv_result TYPE int4. PERFORM fac USING 6 CHANGING lv_result. WRITE: / lv_result. FORM fac USING iv_n TYPE int4 CHANGING cv_result TYPE int4. DATA: lv_n TYPE i. cv_result = lv_n = iv_n. lv_n = lv_n - 1. IF lv_n > 1. PERFORM fac USING lv_n CHANGING cv_result. ENDIF. IF lv_n = 1. cv_result = cv_result * lv_n. ELSE. cv_result = cv_result * iv_n. ENDIF. ENDFORM. And here comes tail recursion version: REPORT ztail. START-OF-SELECTION. DATA: lv_result TYPE int4. PERFORM fac USING 5 1 CHANGING lv_result. WRITE: / lv_result. FORM fac USING iv_n TYPE int4 iv_acc TYPE int4 CHANGING cv_result TYPE int4. DATA: lv_n TYPE i, lv_accumulate TYPE i. IF iv_n < 1. cv_result = 1. ELSEIF iv_n = 1. cv_result = iv_acc * iv_n. ELSEIF iv_n > 1. lv_n = iv_n - 1. lv_accumulate = iv_acc * iv_n. PERFORM fac USING lv_n lv_accumulate CHANGING cv_result. ENDIF. ENDFORM. 本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

优秀的个人博客,低调大师

JavaScript ES6 Fetch API时需要注意的一个Cookie问题

When I am doing a test of comparison between Stateful and Stateless BSP application ( mentioned in blog Stateless and Stateful – Different behavior in application side ), I meet with a strange issue. The conclusion is stateful BSP application will handle request sequentially. Suppose in client I send two request A and B to server. Request A takes 3 seconds to finish and B 2 seconds. The request is sent via jQuery API.It means for stateful application, I will observe the following timeline in Chrome network tab: (1) the start time of both request are almost the same, since I send out two request in client code almost at the same time.(2) even though the second request itself takes 2 seconds to finish, the total processing time for it is 3 seconds waiting for A to finish first + 2 seconds = 5 seconds in the end. The above test did verify the conclusion.However when I change the approach to send request into ES6 fetch API, <%@page language="abap" %> <%@extension name="htmlb" prefix="htmlb" %> <!DOCTYPE html> <html> <head> <title>Jerry Test Stateful</title> </head> <body> <button onclick="fire()">Fire two request</button> <script> function wrapperOnFetch(url){ fetch(url).then(function(response){ return response.json(); }).then(function(json){ console.log(url + ":" + json.message); }); } function fire(){ wrapperOnFetch("first.json"); wrapperOnFetch("second.json"); } </script> </body> </html> the testing request for stateful application looks as below this time: The two requests are handled simultaneously ( request B only takes 2 seconds to finish, no 3 seconds’ wait time for A to finish this time ), the response of second request returns first before the first, which could be observed in console: why the latest ES6 API causes such discrepancy with known conclusion?Just compare the cookie of requests sent via these two kinds of API: Through comparison I get to know that the session cookiesap-contextid is not sent together with request triggered by ES6 Fetch API.Then in Fetch documentation I find out that I need to add option credentials: “include”. function wrapperOnFetch(url){ // enable session cookie sent with request fetch(url,{ credentials:"include" }).then(function(response){ return response.json(); }).then(function(json){ console.log(url + ":" + json.message); }); } After this change the stateful BSP application behaves as expected: the requests are handled in sequence. 本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

用户登录
用户注册
修改密码
忘记密码