# HTMLDialog callbacks

**URL:** <https://forums.sketchup.com/t/htmldialog-callbacks/135827>\
**Category:** Ruby API\
**Created:** [September 9, 2020, 11:07pm UTC](https://forums.sketchup.com/t/htmldialog-callbacks/135827 "2020-09-09T23:07:58Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [September 9, 2020, 11:07pm UTC](https://forums.sketchup.com/t/htmldialog-callbacks/135827/1 "2020-09-09T23:07:58Z")

</div>

> [@Ruby - rubocop Method too long](https://forums.sketchup.com/t/ruby-rubocop-method-too-long/109165/27):
>
> You should be using the `sketchup` object. IE …
> 
> ```auto
> sketchup.do_action("action");
> 
> ```

I noticed today that the new way of sending callbacks is significantly slower when passing a large json object. Does anyone know why?

> **[Class: UI::HtmlDialog](https://ruby.sketchup.com/UI/HtmlDialog.html#add_action_callback-instance_method)**
>
> The Ruby HtmlDialog class allows you to create and interact with HTML dialog boxes from Ruby.

vs this: (both tested on HTML dialog even though this method isn’t documented for HTMLDialog class.)

> **[Class: UI::WebDialog](https://ruby.sketchup.com/UI/WebDialog.html#add_action_callback-instance_method)**
>
> The Ruby WebDialog class allows you to create and interact with DHTML dialog boxes from Ruby.

```auto
t1 = performance.now()
sketchup.save_openings(Openings);
t2 = performance.now()
console.log(t2-t1)
>> 228.46999999997206

t1 = performance.now()
window.location = 'skp:save_openings@' + JSON.stringify(Openings);
t2 = performance.now()
console.log(t2-t1)
>> 16.809999999939464

```

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [September 10, 2020, 3:18am UTC](https://forums.sketchup.com/t/htmldialog-callbacks/135827/2 "2020-09-10T03:18:13Z")

</div>

What is the result units ? seconds? milliseconds?

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [September 10, 2020, 10:52am UTC](https://forums.sketchup.com/t/htmldialog-callbacks/135827/3 "2020-09-10T10:52:53Z")

</div>

Milliseconds.

> **[performance.now() - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/Performance/now)**
>
> The performance.now() method
> returns a DOMHighResTimeStamp, measured in milliseconds.

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [September 10, 2020, 11:16am UTC](https://forums.sketchup.com/t/htmldialog-callbacks/135827/4 "2020-09-10T11:16:26Z")

</div>

Okay, so it’s the difference between a blink of an eye and 7.5% of a blink. 😉

It is interesting however.

What is the time if you do …

```javascript
t1 = performance.now()
sketchup.save_openings(JSON.stringify(Openings));
t2 = performance.now()
console.log(t2-t1)

```

… and on the Rubyside …

```ruby
dialog.add_action_callback('save_openings') {|_unused, openings|
  @my_hash[:openings]= JSON.parse(openings)
  # ... etc., ...
}

```

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [September 10, 2020, 11:20am UTC](https://forums.sketchup.com/t/htmldialog-callbacks/135827/5 "2020-09-10T11:20:25Z")

</div>

As I understand it, calls to Ruby from `UI::HtmlDialog` are asynchronous.  
So testing linear durations like this might not be meaningful.

@tt_su ?

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [September 10, 2020, 11:34am UTC](https://forums.sketchup.com/t/htmldialog-callbacks/135827/6 "2020-09-10T11:34:28Z")

</div>

> [@DanRathbun](#):
>
> What is the time if you do

Interesting…

```javascript
t1 = performance.now()
sketchup.save_openings(JSON.stringify(Openings));
t2 = performance.now()
console.log(t2-t1)
>> 22.330000000001746

t1 = performance.now()
sketchup.save_openings(Openings);
t2 = performance.now()
console.log(t2-t1)
>> 244.33000000000175

```

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [September 10, 2020, 11:38am UTC](https://forums.sketchup.com/t/htmldialog-callbacks/135827/7 "2020-09-10T11:38:59Z")

</div>

Have you tried multiple iterations and finding the min, max and mean ?

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [September 10, 2020, 12:04pm UTC](https://forums.sketchup.com/t/htmldialog-callbacks/135827/8 "2020-09-10T12:04:18Z")

</div>

I ran them both half a dozen times and the times are pretty consistent ±10%.

> [@DanRathbun](#):
>
> Okay, so it’s the difference between a blink of an eye and 7.5% of a blink.

That’s actually what started the investigation. I hate unnecessary lag in any app, and 200ms is usually about where I start to notice (and investigate) lag.

In the meantime I’ve changed the code to only send information about the items that changed instead of sending the whole thing. It’s interesting thought that stringifying is that much faster.

Looks now like I wouldn’t have needed to. Interesting knowledge to store in the back of my mind. At least I now know the `sketchup.method_name` is not the culprit.

```auto
t1 = performance.now()
sketchup.save_openings(JSON.stringify(Openings));
t2 = performance.now()
console.log(t2-t1)
>> 9.390000000130385

```

```auto
t = Time.now
openings = JSON.parse(openings) if openings
puts "#{Time.now - t} seconds to parse openings json."
>> "0.029919 seconds to parse openings json."

```

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [September 10, 2020, 12:39pm UTC](https://forums.sketchup.com/t/htmldialog-callbacks/135827/9 "2020-09-10T12:39:57Z")

</div>

> [@Neil\_Burkholder](#):
>
> It’s interesting thought that stringifying is that much faster.  
> … At least I now know the `sketchup.method_name` is not the culprit.

Actually I think it is. The reason I had you explicitly do the JS side `JSON.stringify` is to bypass (to a certain extent) the JS `sketchup` object’s **argument processing** and just give it a plain `String`, which has always been passed on over to Ruby without change.

The nifty built-in **JS Object `-->` Ruby Hash** process seems to be implemented in a _not so efficient_ way.

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [September 10, 2020, 1:08pm UTC](https://forums.sketchup.com/t/htmldialog-callbacks/135827/10 "2020-09-10T13:08:03Z")

</div>

> [@DanRathbun](#):
>
> The nifty built-in **JS Object `-->` Ruby Hash** process seems to be implemented in a _not so efficient_ way.

We’ll need some input from Trimble on that. @thomthom

```javascript
sketchup.save_openings
< function save_openings() { [native code] }

```

---

<div class="post-metadata">

**Author:** ![tt\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tt_su/32/17_2.png) [@tt\_su](https://forums.sketchup.com/u/tt_su)\
**Post date:** [October 15, 2020, 1:05pm UTC](https://forums.sketchup.com/t/htmldialog-callbacks/135827/11 "2020-10-15T13:05:16Z")

</div>

Hm… that’s interesting. I’m not entirely familiar with the exact nature of the logic involved in passing data from CEF to the app. But the performance difference is certainly worth an investigation.

Do you mind logging an issue in the tracker? [GitHub - SketchUp/api-issue-tracker: Public issue tracker for the SketchUp and LayOut's APIs](https://github.com/SketchUp/api-issue-tracker)

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [October 16, 2020, 1:58pm UTC](https://forums.sketchup.com/t/htmldialog-callbacks/135827/12 "2020-10-16T13:58:18Z")

</div>

> <https://github.com/SketchUp/api-issue-tracker/issues/554>
>
> The HtmlDialog callback methods from JavaScript are significantly slower when pa…ssing on object than when passing a string and parsing it in ruby.
> 
> See here for discussion and benchmarks. 
> 
> \[https://forums.sketchup.com/t/htmldialog-callbacks/135827/6\](https://forums.sketchup.com/t/htmldialog-callbacks/135827/6)

---

<div class="post-metadata">

**Author:** ![gkernan](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@gkernan](https://forums.sketchup.com/u/gkernan)\
**Post date:** [October 16, 2020, 7:18pm UTC](https://forums.sketchup.com/t/htmldialog-callbacks/135827/13 "2020-10-16T19:18:37Z")

</div>

I use some javascript and some jquery. My HTML is essentially embedded and it doesn’t load from disk. I have a huge amount of data in cabmaker.

471 language translations for 537 labels (prompts)  
413 dynamic drop downs  
464 text boxes  
130 check boxes  
All in 9 tabs

These are all loaded and then captions, text boxes, check boxes and drop downs are all resized (different languages require different amount of room.

I ran web dialogs against html dialogs (5 times each) and then took an average

**Web Dialog**  
0.151 seconds to load data (with my own methods)  
2.55 seconds to resize forms and to trigger Ready function.

**HTML Dialog**  
0.126 seconds to load data (with my own methods)  
0.433 seconds to resize forms and to trigger Ready function.

Clients can choose old and new browser as a configuration setting. I run the same code for both versions with a very small part that is version specific.

With the first few versions on htmldialog I was not impressed

but now and needless to say I am quite impressed with the html dialog.

---

<div class="post-metadata">

**Author:** ![gkernan](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@gkernan](https://forums.sketchup.com/u/gkernan)\
**Post date:** [October 16, 2020, 11:46pm UTC](https://forums.sketchup.com/t/htmldialog-callbacks/135827/14 "2020-10-16T23:46:56Z")

</div>

So my findings are that the htmldialog is significantly faster than webdialog  
I use a strategy similar to the old Token ring network. So - even though it is asynchronous - it goes back and forth until done and you don’t loose any timing.

Sorry - I should mention that my desktop computer is 7 years old.
