# Vtk.js with vtk.wasm usage

**URL:** https://discourse.vtk.org/t/vtk-js-with-vtk-wasm-usage/15427
**Category:** vtk.js
**Tags:** vtkwasm
**Created:** [March 25, 2025, 2:53pm UTC](https://discourse.vtk.org/t/vtk-js-with-vtk-wasm-usage/15427 "2025-03-25T14:53:30Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![antonskrebkov](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/cc9497/32.png) [@antonskrebkov](https://discourse.vtk.org/u/antonskrebkov)
#### Post date: [March 25, 2025, 2:53pm UTC](https://discourse.vtk.org/t/vtk-js-with-vtk-wasm-usage/15427/1 "2025-03-25T14:53:30Z")

</div>

Hi!

I faced a lot of `vtk.js` restrictions (mostly lack of necessary methods, filters etc) and decided to expand application with `vtk wasm` but only as computation power without any rendering. Before that the `flask` backend with `python-vtk` was used, but because of speed limits and too long processing time it does not always fit.

First I tried to process _(“process” – send vtp from `vtk.js` to `vtk wasm` for applying e.g. any filter that is missing in `vtk.js` and return this updated vtp to `vtk.js` back)_ some lightweight polydata through `wasm` and it work really much faster then it was through backend.

But what I noticed is when I am trying to process large `vti` file through `wasm` (100+mb) it process much longer than through the backend.

Since I can’t transfer readers and other vtk things from `vtk.js` to `c++ vtk` directly, I have to transfer it as files (strings) and quite a lot of time is spent on read and write these heavyweight files on both `vtk.js` and `vtk wasm` sides.

Are there any problems with this approach and is there a better way to optimize data flow between `vtk.js` and `vtk wasm`?

JS:

```auto
const core = await wasmModuleInstance
const vti = new TextDecoder('utf-8').decode(new Uint8Array(fileData))
const vtiStringPtr = core.allocateUTF8(vti)

const resultPtr = core.ccall(
  'wasmFunctionName',
  'number',
  ['number'],
  [vtiStringPtr]
)

const resultStr = core.UTF8ToString(resultPtr)
const resultVolume = new TextEncoder().encode(resultStr).buffer

core._free(vti)
core._free(vtiStringPtr)
core._free(resultStr)

// regular vtk.js rendering using resultVolume variable as file

```

C++ WASM:

```auto
char* wasmFunctionName(const char* volume) {

  // this operation takes a lot of time if volume is heavyweight
  auto reader = vtkSmartPointer<vtkXMLImageDataReader>::New();
  reader->ReadFromInputStringOn();
  reader->SetInputString(volume);
  reader->Update();

  // ...
  // some operations
  // ...

  // this operation also takes a lot of time if volume is heavyweight
  auto volumeWriter = vtkSmartPointer<vtkXMLImageDataWriter>::New();
  volumeWriter->SetInputData(updatedVolume);
  volumeWriter->WriteToOutputStringOn();
  volumeWriter->Update();
  
  std::string outputStr = volumeWriter->GetOutputString();
  char* resultStr = (char*)malloc(outputStr.size() + 1);
  strcpy(resultStr, outputStr.c_str());
  return resultStr;

```
