Skip to main content
edited tags & title
Source Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191

Smallest job first algorithm implementation in javascript

Function takes jobs array with transaction time and index. Returns the clock cycles of the process/job at specific index. Function Call with sample input, sjf([10, 3, 15, 8, 7], 3)sjf([10, 3, 15, 8, 7], 3).

Smallest job first algorithm implementation in javascript

Function takes jobs array with transaction time and index. Returns the clock cycles of the process/job at specific index. Function Call with sample input, sjf([10, 3, 15, 8, 7], 3).

Smallest job first algorithm implementation

Function takes jobs array with transaction time and index. Returns the clock cycles of the process/job at specific index. Function Call with sample input, sjf([10, 3, 15, 8, 7], 3).

Source Link
Saqib S
  • 165
  • 3

Smallest job first algorithm implementation in javascript

I have implemented smallest job first algorithm using javascript. It was part of a coding challenge. I would like to ask for evaluation of my script. I would like to get suggestions to improve my code performance. Which parts of the code I can improve. Is there an alternative to for loops?

Function takes jobs array with transaction time and index. Returns the clock cycles of the process/job at specific index. Function Call with sample input, sjf([10, 3, 15, 8, 7], 3).

Here is the code.

function sjf(jobs, index) {

// declare waiting time array
var waitingTime = [];
// set waiting time for first task to zero
waitingTime[0] = 0;

// declare total time array
var totalTimeArray = [];

var processArray = [];
// add job value and index as an object to process array
for (i=0; i < jobs.length; i++) {
    processArray.push({
        id: i,
        value: jobs[i]
    })
}

// sort the array in ascending order
function sortArray(a, b) {
    return a.value - b.value;
}

// calculate waiting time for each process except first process
// waiting time for first process is zero so we start to calculate from index 1
function calculateWaitingTime(sortedArray, waitingTime) {
    for (i=1; i < sortedArray.length; i++) {
        waitingTime.push(sortedArray[i-1].value + waitingTime[i-1]);
    }
}

// total time taken to complete each task
function calculateTimeForEachTask(sortedArray, totalTimeArray) {
    for (i=0; i < sortedArray.length; i++) {
        totalTimeArray.push({
            id: sortedArray[i].id,
            time: sortedArray[i].value + waitingTime[i]
        });
    }
}

// find clock cycles
function findClockCycles(totalTimeArray, index) {
    for (i = 0; i < totalTimeArray.length; i++) {
        if (totalTimeArray[i].id === index) {
            return totalTimeArray[i].time;
        }
    }
}

// First of all sort the process array in ascending order of their value
var sortedArray = processArray.sort(sortArray);

// calculate waiting time for the rest of the processes
calculateWaitingTime(sortedArray, waitingTime);


// calculate total time for each task
calculateTimeForEachTask(sortedArray, totalTimeArray);

// return clock cycles for the task
return findClockCycles(totalTimeArray, index);
}