发布
社区首页 >问答首页 >Javascript:如何为JSON数组创建可转移对象

Javascript:如何为JSON数组创建可转移对象
EN

Stack Overflow用户
提问于 2021-04-08 10:22:52
回答 1查看 113关注 0票数 0

我将我现有的一个应用程序转换为使用"worker“从数据库表中获取数据,将数据转换为所需的格式,最后将数据发送到主线程以显示在UI (用户界面)上。

UI所需的数据格式是JSON对象数组。JSON{},JSON{},.....我能够使用post消息将数据从工作线程发送到主线程,该消息使用“结构化克隆”复制方法。我发现使用"worker“来获取数据,比直接在主线程中实现数据获取要花费更多的性能时间。我希望通过使用“可转移对象”,我可能能够带来与在主线程中实现数据获取相同或更好的性能时间。

我尝试使用“可传输对象”将数据从工作线程传输到主线程。但我并不成功,因为只有ArrayBuffer是可以转让的。我从数据库获取的数据,并在worker中进行处理以发送到主线程,主要是文本或字符串数据。获取的每个数据库行都是一个约会,它被转换(在消息之后)为一个JSON对象,如下面的代码所示。我有将近一千个约会要处理。当我将数据作为单个JSON{}发送到主线程时,需要花费相当多的时间。将JSON{}作为数组发送到主线程要快得多。因此,我正在寻找将JSON{}的数组从worker发送到主线程,然后在主线程中检索该数组并依次传递给UI的方法。

有人能解释一下在这种情况下如何使用可转移对象吗?

代码语言:javascript
代码运行次数:0
复制
    let gl_calendarEventArray = [];
    
    //In a loop the array is filled with JSON objects
    
    gl_calendarEventArray.push({
            id: appointment.appointment_id,
            clientIssueFlag: appointment.clientIssueFlag,
            apptIssueFlag: appointment.apptIssueFlag,
            issueTypeFlag: appointment.issueTypeFlag,
            recur_appt_id: appointment.recurring_appointment_id,
            appt_type_id: appointment.appointment_type_id,
            branch_id: appointment.branch_id,
            resourceId: appointment.tutor_id,
            student_branch_id:appointment.student_branch_id,
            student_id: appointment.student_id,
            title: title,
            tutor_id: appointment.tutor_id,
            tutor: appointment.tutor_name,
            service: appointment.service_name,
            student: appointment.student_name,
            start: appointment.start_date_time,
            demo: appointment.demo,
            end: appointment.end_date_time,
            status_id: appointment.appointment_status_id,
            duration: duration,
            area_of_focus: area_of_focus,
            student_notes: student_notes,
            appointment_notes: appointment_notes,
            primary_parent_name: primary_parent_name,
            primary_parent_email: primary_parent_email,
            primary_parent_phone: primary_parent_phone,
            secondary_parent_name: secondary_parent_name,
            secondary_parent_email: secondary_parent_email,
            secondary_parent_phone: secondary_parent_phone,
            current_grade_level: current_grade_level,
            school_name: school_name,
            color: color,
            textColor: textColor,
            additional_info:additional_info
        });         

//After the loop is completed, the data is sent to the main-thread
//by a postMessage which is using the structured copy now.

self.postMessage({msgType: 'CalendarEvent', msg: gl_calendarEventArray});
EN

回答 1

Stack Overflow用户

发布于 2021-04-10 11:35:32

简单地说,你不需要。

只有可转移对象才能被转移,而普通的JS对象不能被转移。

如果我们确实想“传输”一些数据,我们必须首先将这些对象序列化成一种适合ArrayBuffer的形式(因为其他可传输对象不能容纳任意数据),如果我们懒惰,我们可以使用JSON将其串行化,然后将该字符串转换为ArrayBuffer,例如通过TextEncoder。如果我们有时间和资源,并且我们的格式非常适合它,我们还可以构建我们自己的序列化程序和解析器。

然后,我们将序列化的表单传输到另一端,后者仍然必须将其解析回JS对象。

因此,我们不是只有两个JS对象,而是两个JS对象+一个保存序列化数据的ArrayBuffer……(如果我们懒惰并使用JSON,还会有另外两个字符串表示)。这并不是我所说的优化...

transferables的“魔力”在于,你可以让一个线程中的内存插槽被另一个线程直接访问,从而避免内存膨胀和无用的垃圾回收。然而,传输可转移对象并不一定比克隆它们更快,而且自己进行序列化+解析肯定会比浏览器的内部结构化克隆性能差得多。

但是,如果您在该工作线程中所做的所有操作实际上只是获取数据,并且没有CPU密集型任务,那么只需从主上下文执行,获取是并行完成的,并且不是CPU密集型任务。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66996522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档