v8

Why in this case native promises seems to be faster than callbacks in chrome?

筅森魡賤 提交于 2019-12-05 22:09:06
Here's the jsperf: http://jsperf.com/promise-vs-callback callback case (211 Ops/s): // async test var d = deferred; function getData(callback) { setTimeout(function() { callback('data') }, 0) } getData(function(data) { d.resolve() }) Promise case(614 ops/s): // async test var d = deferred; function getData() { return new Promise(function(resolve) { setTimeout(function() { resolve('data') }, 0); }) } getData().then(function(data) { d.resolve() }) As you see promise are way faster, but they have more code. The question is why this happens. Here deferred is to defined by jsperf to show it as the

C++ - Qt + v8 under msvc2012

半城伤御伤魂 提交于 2019-12-05 21:29:23
Recently, I've started a project under Qt5.1.0 . After some development, I choose to make a scripting system under Javascript with Google V8 . Under Windows 7 x64 , the only way to compile V8 is under msvc2012 , and I got 3 .lib files to use. In a single project using ONLY V8 , everything works well. But integrating V8 with an existent project using Qt5 it's a bit more complicated. Here is an example of a minimal code I'm using : (Of course, there is more file in this project...) #include <QApplication> #include <v8.h> using namespace v8; int v8_test() { Isolate* isolate = Isolate::GetCurrent(

How to handle V8 engine crash when process runs out of memory

左心房为你撑大大i 提交于 2019-12-05 20:48:28
问题 Both node console and Qt5's V8-based QJSEngine can be crashed by the following code: a = []; for (;;) { a.push("hello"); } node's output before crash: FATAL ERROR: JS Allocation failed - process out of memory QJSEngine 's output before crash: # # Fatal error in JS # Allocation failed - process out of memory # If I run my QJSEngine test app (see below) under a debugger, it shows a v8::internal::OS::DebugBreak call inside V8 code. If I wrap the code calling QJSEngine::evaluate into __try-_

成就现代Web,JavaScript成为编程领域最流行的语言之一

有些话、适合烂在心里 提交于 2019-12-05 19:57:14
如果你在 1999 年预测,20 年后 JavaScript 会成为编程领域里最流行的语言之一,那你要么是通灵未来,要么就是精神错乱。 但是,JavaScript 从诞生之初作为 Web 开发的替补工具,不断发展并打败挑战者,在成就现代 Web 的同时,发展成为了伟大的编程语言之一。​ 世纪之交的 JavaScript 不仅无法做到专业语言能够做到的,它甚至没有被设计为一种严肃的编码工具。毕竟,需要创建 Web 应用程序的开发者已经拥有了他们喜欢使用的程序工具——Java,通过applet 嵌入系统的方式。 但是在 1995 年,具有开拓精神的 Web 浏览器公司 Netscape 就已经意识到他们需要为人们提供一种更简单的选项来创建 Web 应用程序。他们处在一个困难的节点——与微软在市场上开战,与 Sun Microsystems 处在关闭主要战略合作关系的边缘,而且时间紧迫。他们雇佣Brendan Eich在一个几乎不可能的严苛时间线内创造一种新语言。Brendan Eich 在 10 天内打磨出 JavaScript 的第一个版本,正好赶上了这个漂亮的浏览器: Netscape 2: JavaScript 初次亮相的舞台 Eich 真正想要做的是构建一个浏览器承载的类似Scheme 的学术性的编程语言。但是 Netscape 有一个不同的愿景。他们想要一个看起来像 Java

Converting Python objects to JavaScript for PyV8

孤街醉人 提交于 2019-12-05 19:17:04
I'm trying to pass Python data (lists, dicts, strings..., arbitrarily nested) to PyV8. class Global(object): def __init__(self, data): self.data = data ctx = PyV8.JSContext(Global([{'a':1}])) ctx.enter() res = ctx.eval('data.length') js_len = PyV8.convert(res) print js_len The code above prints None, presumably because the data object is not transformed to a JSArray and thus data.length evaluates to undefined . Is there a reliable way to do the necessary conversion in PyV8 other than using JSON? Michel Müller Apparently PyV8 doesn't correctly convert python lists to Javascript arrays, which

How to add a new class to Google V8?

好久不见. 提交于 2019-12-05 16:57:15
问题 I'm a new comer in Google V8 and Javascript, and I'm trying to add a new class to Javascript using C++. I've finished some work using Webkit's V8 binding, references are: webkit idl and v8 binding Now I want to integrate it into V8 engine directly, by modifying V8's code instead of simply using V8's api to make a extension. In other words, I want to add a new class just like Array type in Javascript, using the same implementation mechanism. I've searched the Internet, including docs in Google

Nodejs的运行原理-架构篇

谁说我不能喝 提交于 2019-12-05 15:34:40
前言 本来是想只做一个Nodejs运行原理-科普篇,但是收到了不少私信,要我多分享一些更进阶,更详细的内容,所以我会在接下来的两个月里继续更新Nodejs运行原理。 PS:此系列只做Nodejs的运行原理(架构,libuv,v8 etc),并不介绍Nodejs功能以及使用方法。 本文以两个view来看Nodejs的架构,一个是从模块依赖的角度,另一个是从函数调用的角度。 1.模块依赖 如上图所示: your code 为编辑代码, node.js 核心, Host environment 为宿主环境(提供各种服务,如文件管理,多线程,多进程,IO etc) 1.1node.js 这里重点介绍 ,nodejs组成部分: v8 engine, libuv, builtin modules, native modules 以及其他辅助服务。 v8 engine :主要有两个作用 1.虚拟机的功能,执行js代码(自己的代码,第三方的代码和native modules的代码)。              2.提供C++函数接口,为nodejs提供v8初始化,创建context,scope等。 libuv :它是基于事件驱动的异步IO模型库,我们的js代码发出请求,最终由libuv完成,而我们所设置的回调函数则是在libuv触发。 builtin modules :它是由C++代码写成各类模块

Can this function be garbage-collected?

房东的猫 提交于 2019-12-05 15:09:17
问题 Consider this piece of cake... ehm, code: 'use strict' function doWork () { return new Promise(function (resolve, reject) { // work work work... // Done! But... where's the resolve() ??? }) } doWork().then(function doMoreWork () { // Some more work to do... }) Once the function in the Promise's constructor finishes... Is the Promise object garbage-collectible? Is doMoreWork() garbage-collectible? My guess is that doMoreWork() cannot be GC-ed directly because the Promise keeps a reference to

NodeJS内存控制

泪湿孤枕 提交于 2019-12-05 14:32:18
在一般的后端语言中是没有内存使用限制的,但是Node中通过JS使用内存时智能使用部分内存,64bit=>1.4G,32bit=>0.7G,所以Node无法直接操作大内存对象,之所以会有这种现象,是因为Node是基于V8引擎构建,V8对于JS对象是通过自己的方式来分配和管理的。至于V8为何要限制内存,其实是出于两方面考虑的一是当时V8面向的是浏览器,大量内存的使用场景很少,二是垃圾回收机制的限制。当然这个限制可以设置。 process.memoryUsege() 查看内存使用情况。 1. 内存相关参数整理 node 启动时添加 --max-old-space-size 新生代空间 --max-new-space-size 老生代空间 --trace_gc -e 垃圾回收日志 2. 大内存的应用 // method1 var reader = fs.createReadStream('in.txt'); var writer = fs.createWriteStream('out.txt'); reader.on('data', function(chunk){ writer.write(chunk); } reader.on('end', function(){ writer.end(); } // method2 var reader = fs.createReadStream(

nodejs v8.getHeapStatistics method

随声附和 提交于 2019-12-05 13:30:23
问题 In nodejs v8 module, there's a function called getHeapStatistics which return an object that contains information about memory usage: { total_heap_size: 221540352, total_heap_size_executable: 5242880, total_physical_size: 221540352, total_available_size: 1286110104, used_heap_size: 189179192, heap_size_limit: 1501560832, malloced_memory: 16384, peak_malloced_memory: 1325112, does_zap_garbage: 0 } What's the meaning of each field? 回答1: Some good explanation from gc-heap-stats package: total