IT商业网-解读信息时代的商业变革
当前位置: 首页 > 软件 > 正文

JavaScript 引擎 V8 发布 8.5 版本 带来Promise.any等新亮点

2020-07-24 09:40:23  来源:开源中国    

  JavaScript 引擎 V8 发布了 8.5 版本(测试阶段),正式版本将在之后随 Chrome 85 一起推出。8.5 版本带来了一些面向开发人员的特性,主要亮点包括:

  JavaScript

  Promise.any 和 AggregateError:

  Promise.any 是一个 Promise 组合器,一旦输入的一个 Promise 满足,它就会解决所产生的 Promise。

  const promises = [

  fetch('/endpoint-a').then(() => 'a'),

  fetch('/endpoint-b').then(() => 'b'),

  fetch('/endpoint-c').then(() => 'c'),

  ];

  try {

  const first = await Promise.any(promises);

  // Any of the promises was fulfilled.

  console.log(first);

  // → e.g. 'b'

  } catch (error) {

  // All of the promises were rejected.

  console.assert(error instanceof AggregateError);

  // Log the rejection values:

  console.log(error.errors);

  }

  如果所有输入的 Promise 都被拒绝,则所产生的 Promise 将被 AggregateError 对象拒绝,该对象包含一个 error 属性,该属性保存一个拒绝值数组。

  String.prototype.replaceAll:

  String.prototype.replaceAll 提供了一种简单的方法来替换所有出现的子字符串,而无需创建全局 RegExp。

  const queryString = 'q=query+string+parameters';

  // Works, but requires escaping inside regular expressions.

  queryString.replace(/\+/g, ' ');

  // → 'q=query string parameters'

  // Simpler!

  queryString.replaceAll('+', ' ');

  // → 'q=query string parameters'

  Logical assignment operators(逻辑赋值运算符)

  逻辑赋值运算符是新的复合赋值运算符,它将逻辑运算符 &&、 || 或 ?? 与任务组合在一起

  x &&= y;

  // Roughly equivalent to x && (x = y)

  x ||= y;

  // Roughly equivalent to x || (x = y)

  x ??= y;

  // Roughly equivalent to x ?? (x = y)

  与数学和按位复合赋值运算符不同,逻辑赋值运算符仅有条件地执行赋值。

  WebAssembly

  在所有平台上都提供了 Liftoff

  从 V8 v6.9 开始,Liftoff 一直用作 Intel 平台上 WebAssembly 的基准编译器(Chrome 69 在桌面系统上启用了它)。由于担心内存的增加(基线编译器会生成更多代码),因此到目前为止一直将其保留在移动系统中。

  经过最近几个月的试验,v8 团队发现大多数情况下的内存增加可以忽略不计,因此最终在所有架构上默认都启用了 Liftoff,从而提高了编译速度,尤其是在 ARM 设备(32 位和 64 位)上。

  提供多值(Multi-value)支持

  现在,WebAssembly 对多值代码块和函数返回的支持已可以普遍使用。这反映了该提案最近在正式的 WebAssembly 标准中的合并,并得到所有编译层的支持。

  例如,现在这是一个有效的 WebAssembly 函数:

  (func $swap (param i32 i32) (result i32 i32)

  (local.get 1) (local.get 0)

  )

  如果导出了该函数,则也可以从 JavaScript 对其进行调用,并返回一个数组:

  instance.exports.swap(1, 2);

  // → [2, 1]

  相反,如果 JavaScript 函数返回数组(或任何迭代器),则可以将其导入并在 WebAssembly 模块内作为多返回函数调用:

  new WebAssembly.Instance(module, {

  imports: {

  swap: (x, y) => [y, x],

  },

  });(func $main (result i32 i32)

  i32.const 0

  i32.const 1

  call $swap

  )

  更重要的是,工具链现在可以使用此功能在 WebAssembly 模块中生成更紧凑、更快的代码。

  支持 JS BigInts

  已提供 WebAssembly 支持,用于将 WebAssembly I64 值与 JavaScript BigInts 相互转换,并且根据官方标准的最新更改,该支持可用于一般用途。

  现在可以从 JavaScript 调用具有 i64 参数和返回值的 WebAssembly 函数,而不会造成精度损失:

  (module

  (func $add (param $x i64) (param $y i64) (result i64)

  local.get $x

  local.get $y

  i64.add)

  (export "add" (func $add)))

  从 JavaScript,只能将 BigInts 作为 I64 参数传递:

  WebAssembly.instantiateStreaming(fetch('i64.wasm'))

  .then(({ module, instance }) => {

  instance.exports.add(12n, 30n);

  // → 42n

  instance.exports.add(12, 30);

  // → TypeError: parameters are not of type BigInt

  });

原标题:JavaScript 引擎 V8 发布 8.5 版本

免责声明: IT商业新闻网遵守行业规则,本站所转载的稿件都标注作者和来源。 IT商业新闻网原创文章,请转载时务必注明文章作者和来源“IT商业新闻网”, 不尊重本站原创的行为将受到IT商业新闻网的追责,转载稿件或作者投稿可能会经编辑修改或者补充, 如有异议可投诉至:post@itxinwen.com
微信公众号:您想你获取IT商业新闻网最新原创内容, 请在微信公众号中搜索“IT商业网”或者搜索微信号:itxinwen,或用扫描左侧微信二维码。 即可添加关注。
标签:

品牌、内容合作请点这里: 寻求合作 ››

相关阅读RELEVANT