<!DOCTYPE html>
<title>SAPUI5</title>
<script src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js" id="sap-ui-bootstrap" data-sap-ui-theme="sap_bluecrystal" data-sap-ui-libs="sap.m" data-sap-ui-bindingSyntax="complex" data-sap-ui-compatVersion="edge" data-sap-ui-preload="async"></script>
<script id="myView" type="ui5/xmlview">
<mvc:View controllerName="MyController" xmlns="sap.m" xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc"
xmlns:l="sap.ui.layout">
<Shell id="shell">
<App id="app">
<pages>
<Page id="page" title="Coding Challenge 2 (with Web Workers)">
<l:VerticalLayout>
<l:HorizontalLayout>
<Input id="_idNumber" width="auto" value="1000000" type="Number"/>
<Input width="350px" placeholder="enter size for each thread..(default 100000)" id="idThreshold" visible="false" type="Number"/>
</l:HorizontalLayout>
<l:HorizontalLayout>
<Button text="Start Processing" press="process"/>
</l:HorizontalLayout>
<CheckBox text="In Background (using Web Workers) ?" select="onCBSelect" id="idBgCb" selected="false"/>
<l:HorizontalLayout>
<Label text="Number having maximum steps "/>
<Label id="maxNum" design="Bold"/>
</l:HorizontalLayout>
<l:HorizontalLayout>
<Label text="Maximum Steps "/>
<Label id="maxSteps" design="Bold"/>
</l:HorizontalLayout>
<l:HorizontalLayout>
<Label text="Time Taken "/>
<Label id="timeTaken" design="Bold"/>
</l:HorizontalLayout>
<Text/>
<Text/>
<Text text = "This is my another submission for SAP Community Coding Challenge No. 2. In this aproach you can avoid the processing of
the large number in the main thread, instead you can use multiple web-workers on small chunks of data running parallely."/>
<Text text="In most cases Parallel-Processing can boost the performance, but unfortunately in this case it doesn't. This is...
JavaScript
sap.ui.getCore().attachInit(function() {
"use strict";
var w;
var startTime1, endTime1;
sap.ui.controller("MyController", {
onInit: function() {
this.process();
},
onCBSelect: function() {
if (this.getView().byId("idBgCb").getSelected()) {
this.getView().byId("idThreshold").setVisible(true);
} else {
this.getView().byId("idThreshold").setVisible(false);
}
},
divideInput: function(input, divisor) {
var splittedData = [];
//Divide this number into chunks of 50,000 each like
// 1 -> 50000 || 50001 -> 100000 || 100001 -> 150000 etc..
if (input <= divisor) {
//No need of splitting
splittedData.push("1/" + input);
} else {
var low = 1,
high = divisor;
while (high < input) {
splittedData.push(low + "/" + high);
low = low + divisor;
high = high + divisor;
}
splittedData.push(low + "/" + input);
}
return splittedData;
},
showResult: function(result) {
sap.m.MessageToast.show("Done " + result);
},
startWorker: function() {
var that = this;
startTime1 = window.performance.now();
if (typeof(Worker) === "undefined") {
sap.m.MessageToast.show("Web Workers are not supported in your browser!!");
return;
}
var maxSteps = 1; //Answer
var maxNum = 1; //Answer
var j = 0; //To keep track of jobs by workers
//Get the input
var numberInQuestion = parseInt(this.getView().byId("_idNumber").getValue(), 10);
//Divide it into smaller parts
var divisorInput = this.getView().byId("idThreshold").getValue();
if (divisorInput === "") divisorInput = 100000;
var dividedInput = this.divideInput(numberInQuestion, parseInt(divisorInput, 10));
//Now for each entry in the above array, set up a worker to get you the answer of that...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!